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
Create a string variable called
sentenceand assign a sentence to it. For example,"Java is a programming language".String sentence = "Java is a programming language";Use the
split()method to split thesentencestring into an array of strings based on the space character. Store the result in a string array variable calledwords.String[] words = sentence.split(" ");Print each element of the
wordsarray 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]); }Save the file and close the text editor.
Compile the
StringSplitLab.javafile.javac StringSplitLab.javaRun the
StringSplitLabprogram.java StringSplitLab
You should see the following output:
The words are:
Java
is
a
programming
language
Split a sentence with a delimiter
Create a string variable called
sentenceand 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";Use the
split()method to split thesentencestring into an array of strings based on the letter"o". Store the result in a string array variable calledwords.String[] words = sentence.split("o");Print each element of the
wordsarray 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]); }Save the file and close the text editor.
Compile the
StringSplitLab.javafile.javac StringSplitLab.javaRun the
StringSplitLabprogram.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
Create a string variable called
sentenceand assign a sentence to it. For example,"I love to code in Java".String sentence = "I love to code in Java";Use the
split()method to split thesentencestring into an array of strings based on the space character with a limit of3. Store the result in a string array variable calledwords.String[] words = sentence.split(" ", 3);Print each element of the
wordsarray 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]); }Save the file and close the text editor.
Compile the
StringSplitLab.javafile.javac StringSplitLab.javaRun the
StringSplitLabprogram.java StringSplitLab
You should see the following output:
The words are:
I
love
to code in Java
Split a sentence with regex
Create a string variable called
sentenceand 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?";Use the
split()method to split thesentencestring 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 calledwords.String[] words = sentence.split("\\W+");Print each element of the
wordsarray 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]); }Save the file and close the text editor.
Compile the
StringSplitLab.javafile.javac StringSplitLab.javaRun the
StringSplitLabprogram.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.



