⬅ Previous Topic
Java StringreplaceFirst()
methodNext Topic ⮕
Java StringstartsWith()
methodsplit()
method⬅ Previous Topic
Java StringreplaceFirst()
methodNext Topic ⮕
Java StringstartsWith()
methodThe split()
method in Java is a powerful tool for breaking down strings into smaller pieces based on a defined delimiter. Think of it like cutting a cake - you use a knife (the delimiter) to separate the cake (your string) into slices (substrings).
public String[] split(String regex)
public String[] split(String regex, int limit)
Parameter | Description |
---|---|
regex |
The regular expression that defines the delimiter. This is what the string will be split on. |
limit |
An optional parameter specifying the maximum number of substrings to return. If limit is provided and less than the number of potential splits, then the last substring will contain the remainder of the string. |
The split()
method returns an array of strings, where each element represents a substring that was separated by the delimiter.
This example demonstrates how to split a string using a simple comma (,) as the delimiter. We'll take a list of fruits and break it into individual fruit names.
String fruits = "apple,banana,orange,grape";
String[] fruitArray = fruits.split(",");
for (String fruit : fruitArray) {
System.out.println(fruit);
}
apple
banana
orange
grape
In this code, the split()
method divides the string "apple,banana,orange,grape"
into four substrings using the comma as a separator. The resulting array fruitArray
contains each fruit name.
This example shows how to split a string by spaces. Let's say you have a sentence, and you want to get individual words from it.
String sentence = "This is a sample sentence";
String[] words = sentence.split(" ");
for (String word : words) {
System.out.println(word);
}
This
is
a
sample
sentence
Here, we split the string "This is a sample sentence"
using a single space as the delimiter. The resulting array words
contains each word in the sentence.
You can use regular expressions for more complex delimiters. This example splits a string by either a comma or a semicolon. Let’s say you have data separated by both commas and semicolons, and you want to treat them as equivalent separators.
String data = "apple,banana;orange,grape";
String[] items = data.split("[;,]");
for (String item : items) {
System.out.println(item);
}
apple
banana
orange
grape
The regular expression "[;,]"
matches either a comma or a semicolon. The split method divides the string at these two characters, creating an array of individual items.
This example uses the limit
parameter to control how many substrings are returned. We'll split a sentence but only get the first two parts.
String longSentence = "This is a very long sample sentence";
String[] firstTwoWords = longSentence.split(" ", 2);
for (String word : firstTwoWords) {
System.out.println(word);
}
This
is a very long sample sentence
Here, we split the string using space as delimiter and setting limit
to 2. The array firstTwoWords
will only contain the first two words of the original sentence followed by everything else concatenated into one single string.
⬅ Previous Topic
Java StringreplaceFirst()
methodNext Topic ⮕
Java StringstartsWith()
methodYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.