Java String Split

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn about the Java string split() method. We will learn how to use the split() method to split a string into an array of strings based on the delimiter or a regular expression. This lab is divided into several steps, and each step will help you to understand the split() method.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/for_loop("`For Loop`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117960{{"`Java String Split`"}} java/identifier -.-> lab-117960{{"`Java String Split`"}} java/arrays -.-> lab-117960{{"`Java String Split`"}} java/data_types -.-> lab-117960{{"`Java String Split`"}} java/for_loop -.-> lab-117960{{"`Java String Split`"}} java/operators -.-> lab-117960{{"`Java String Split`"}} java/output -.-> lab-117960{{"`Java String Split`"}} java/strings -.-> lab-117960{{"`Java String Split`"}} java/variables -.-> lab-117960{{"`Java String Split`"}} java/string_methods -.-> lab-117960{{"`Java String Split`"}} java/system_methods -.-> lab-117960{{"`Java String Split`"}} end

Split a sentence into words

  1. Create a string variable called sentence and assign a sentence to it. For example, "Java is a programming language".

    String sentence = "Java is a programming language";
  2. Use the split() method to split the sentence string into an array of strings based on the space character. Store the result in a string array variable called words.

    String[] words = sentence.split(" ");
  3. Print each element of the words array using a for loop. Add a message like "The words are:" before printing the elements of the array.

    System.out.println("The words are:");
    for (int i = 0; i < words.length; i++) {
        System.out.println(words[i]);
    }
  4. Save the file and close the text editor.

  5. Compile the StringSplitLab.java file.

    javac StringSplitLab.java

  6. Run the StringSplitLab program.

    java StringSplitLab

You should see the following output:

The words are:
Java
is
a
programming
language

Split a sentence with a delimiter

  1. Create a string variable called sentence and assign a sentence to it. For example, "The quick brown fox jumps over the lazy dog".

    String sentence = "The quick brown fox jumps over the lazy dog";
  2. Use the split() method to split the sentence string into an array of strings based on the letter "o". Store the result in a string array variable called words.

    String[] words = sentence.split("o");
  3. Print each element of the words array using a for loop. Add a message like "The words are:" before printing the elements of the array.

    System.out.println("The words are:");
    for (int i = 0; i < words.length; i++) {
        System.out.println(words[i]);
    }
  4. Save the file and close the text editor.

  5. Compile the StringSplitLab.java file.

    javac StringSplitLab.java

  6. Run the StringSplitLab program.

    java StringSplitLab

You should see the following output:

The words are:
The quick br
wn f
x jumps
ver the lazy d
g

Split a sentence with a limit

  1. Create a string variable called sentence and assign a sentence to it. For example, "I love to code in Java".

    String sentence = "I love to code in Java";
  2. Use the split() method to split the sentence string into an array of strings based on the space character with a limit of 3. Store the result in a string array variable called words.

    String[] words = sentence.split(" ", 3);
  3. Print each element of the words array using a for loop. Add a message like "The words are:" before printing the elements of the array.

    System.out.println("The words are:");
    for (int i = 0; i < words.length; i++) {
        System.out.println(words[i]);
    }
  4. Save the file and close the text editor.

  5. Compile the StringSplitLab.java file.

    javac StringSplitLab.java

  6. Run the StringSplitLab program.

    java StringSplitLab

You should see the following output:

The words are:
I
love
to code in Java

Split a sentence with regex

  1. Create a string variable called sentence and assign a sentence to it. For example, "The quick brown fox... jumps over the lazy dog?".

    String sentence = "The quick brown fox... jumps over the lazy dog?";
  2. Use the split() method to split the sentence string into an array of strings based on a regular expression "\\W+" which means any non-word character. Store the result in a string array variable called words.

    String[] words = sentence.split("\\W+");
  3. Print each element of the words array using a for loop. Add a message like "The words are:" before printing the elements of the array.

    System.out.println("The words are:");
    for (int i = 0; i < words.length; i++) {
        System.out.println(words[i]);
    }
  4. Save the file and close the text editor.

  5. Compile the StringSplitLab.java file.

    javac StringSplitLab.java

  6. Run the StringSplitLab program.

    java StringSplitLab

You should see the following output:

The words are:
The
quick
brown
fox
jumps
over
the
lazy
dog

Summary

In this lab, we learned how to use the split() method to split a string into an array of strings based on the delimiter or a regular expression in Java. We practiced splitting a sentence into words, splitting a sentence with a delimiter, splitting a sentence with a limit, and splitting a sentence with a regular expression. The split() method is a powerful tool to work with strings, and these exercises will help you to get hands-on experience with it.

Other Java Tutorials you may like