Skip to main content
TopMiniSite

Back to all posts

How to Split A String With A Space In Java?

Published on
4 min read

Table of Contents

Show more
How to Split A String With A Space In Java? image

To split a string with a space in Java, you can use the built-in split() method of the String class. The split() method allows you to divide a string into an array of substrings based on a given delimiter or regular expression.

To split a string with a space specifically, you need to pass a space character (" ") as the argument to the split() method. Here's an example:

String text = "Hello World!";

String[] words = text.split(" ");

for (String word : words) { System.out.println(word); }

In this example, the split() method is called on the text string using a space as the delimiter. It returns an array of strings words containing each word in the original sentence. The for loop is then used to iterate over the words array and print each word individually.

The output of this example will be:

Hello World!

By splitting the string with a space using the split() method, you can conveniently separate a sentence or any other string into individual words.

Why do we split a string with a space in Java?

We split a string with a space in Java when we want to separate words or elements within the string into individual pieces. This can be useful for tasks such as parsing sentences, extracting keywords, or tokenizing text.

By using the split() method in Java, we can split a string into an array of substrings based on a specified delimiter. When splitting with a space as the delimiter, each word within the string is treated as a separate substring.

For example, consider the following code snippet:

String sentence = "The quick brown fox"; String[] words = sentence.split(" ");

In this case, the split(" ") method will split the string sentence at every occurrence of a space character. The resulting array words will contain the individual words from the original sentence:

words[0] → "The" words[1] → "quick" words[2] → "brown" words[3] → "fox"

Splitting a string with a space is a common approach when dealing with text manipulation and processing, as it allows us to analyze and work with words individually.

Can you split a string and store the substrings in a list in Java?

Yes, you can split a string into substrings and store them in a list using the split() method and the Arrays.asList() method in Java.

Here is an example:

String str = "Hello,World,Java"; String[] substrings = str.split(","); List substringsList = Arrays.asList(substrings);

System.out.println(substringsList);

Output:

[Hello, World, Java]

In this example, the split() method is used to split the string "Hello,World,Java" into substrings based on the comma delimiter (","). The resulting substrings are stored in an array. Then, the Arrays.asList() method is used to convert the array into a list. Finally, the list of substrings is printed to the console.

Is it possible to split a string into equal length substrings in Java?

Yes, it is possible to split a string into equal length substrings in Java. One way to achieve this is by using the substring method in a loop.

Here's an example code that splits a string into equal length substrings:

public class SplitStringExample { public static void main(String[] args) { String str = "abcdefghi"; int length = 3; // Desired substring length

    for (int i = 0; i < str.length(); i += length) {
        String substring = str.substring(i, Math.min(i + length, str.length()));
        System.out.println(substring);
    }
}

}

Output:

abc def ghi

In this example, the substring method is used to extract substrings of length length from the original string str. The loop iterates over the original string, starting at index 0 and incrementing by the desired length. The Math.min(i + length, str.length()) ensures that the last substring is correctly handled when its length is less than the desired length.