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.

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