In java you might face a situation where you need to split String, based on some criteria. To fulfill this requirement Java has already provided two in-built methods to achieve this.
- Method 1: split(String regex, int limit)
- Method 2: split(String regex) (Internally calls above method with the limit as 0)
- split(String regex, 0)
Parameter
regex (Regular Expression): An expression(pattern) around which you would like to split given string.
limit: It will decide how many times a given pattern will be applied while splitting the string.
- n>0: pattern will be applied at most n-1 times.
- n=<0: Pattern will be applied as many times as possible
Return Value
String[]: Array of Strings resulted after splitting string with above regular expression. The length of this array is depended on the limit parameter.
Example
package com.jbt;
/*
* @Author : Javabeginnerstutorial.com
* @Purpose : How to split a string in java
*/
public class java_string_split {
public static void main(String[] args) {
/*
* Here Space will be used as pattern to
* split the string.
*/
final String REGULAR_EXPRESSION_EMPTY_STRING = " ";
/*
* Here "H\w" regular expression will be
* used as pattern to split the string.
*
* \w : It matches word
* characters(alphanumeric characters plus
* underscore).
*
* Given regular expression will match all
* word containing H and single character
* after it.
*/
final String REGULAR_EXPRESSION_SINGLE_WORD_CHARACTER = "H\\w";
String jbtTest = "Hi Hello How Are You!";
String[] strArr = jbtTest.split(REGULAR_EXPRESSION_EMPTY_STRING);
String[] strArr1 = jbtTest.split(REGULAR_EXPRESSION_SINGLE_WORD_CHARACTER);
for (String splitString : strArr){
System.out.println(splitString);
}
System.out.println("###########################################");
for (String splitString : strArr1) {
System.out.println(splitString);
}
}
}
Output
Hi
Hello
How
Are
You!
###########################################
llo
w Are You!
Limit Option Example
package com.jbt;
/*
* @Author : Javabeginnerstutorial.com
* @Purpose : How to split a string in java
*/
public class java_string_split {
public static void main(String[] args) {
/*
* Here Space will be used as pattern to
* split the string.
*/
final String REGULAR_EXPRESSION_EMPTY_STRING = " ";
String jbtTest = "Hi Hello How Are You!";
//It will apply SPACE pattern 0(n-1) times.
String[] strArr = jbtTest.split(REGULAR_EXPRESSION_EMPTY_STRING, 1);
//It will apply SPACE pattern 1(n-1) times.
String[] strArr1 = jbtTest.split(REGULAR_EXPRESSION_EMPTY_STRING, 2);
//It will apply SPACE pattern 2(n-1) times.
String[] strArr2 = jbtTest.split(REGULAR_EXPRESSION_EMPTY_STRING, 3);
for (String splitString : strArr) {
System.out.println(splitString);
}
System.out.println("###########################################");
for (String splitString : strArr1) {
System.out.println(splitString);
}
System.out.println("###########################################");
for (String splitString : strArr2) {
System.out.println(splitString);
}
}
}
Output
Hi Hello How Are You!
###########################################
Hi
Hello How Are You!
###########################################
Hi
Hello
How Are You!