Java String Split Best Practices


Java split a string into an array of strings

The most known way to split a string into an array of strings is split() method.

Public String [ ] split ( String delimiter, int limit )
 Parameters:
 delimiter:
       - delimiting characters or regular expression Limit 
       - the result threshold Returns: An array of strings computed by splitting the given string. 
 Throws: PatternSyntaxException - if the provided regular expression’s syntax is invalid. 

 

StringTokenizer class vs. String.split method in Java

StringTokenizer seems to be more performant and faster than String.split(), mainly because StringTokenizer is more restrictive than split().

But for most cases Split provides the result in milliseconds even for thousands of strings.

You may just use split(), it will for sure be the right choice.

How to split a string in java with delimiter

Java split string by space

/* String to split. */ 
String stringToSplit = "java best practices"; 
String[] tempArray; 

/* delimiter */ 
String delimiter = " "; 

/* given string will be split by the argument delimiter provided. */ 
tempArray = stringToSplit.split(delimiter); 

/* print substrings */ 
for (int i = 0; i < tempArray.length; i++) 
           System.out.println(tempArray[i]); 

Output: 
java 
best 
practices

 

Limit the length of the resulting array

If you want to limit the number of strings in the result, you can use the second parameter to specify this.

/* String to split. */ 
String stringToSplit = "java best practices"; 
String[] tempArray; 

/* delimiter */ 
String delimiter = " "; 

/* given string will be split by the argument delimiter provided. */ 
tempArray = stringToSplit.split(delimiter,2); 

/* print substrings */ 
for (int i = 0; i < tempArray.length; i++) 
           System.out.println(tempArray[i]); 

Output: 
java 
best practices

 

Split a string by comma in Java

/* String to split. */ 
String stringToSplit = "java, string split, best practices"; 
String[] tempArray; 

/* delimiter */ 
String delimiter = ","; 

/* given string will be split by the argument delimiter provided. */ 
tempArray = stringToSplit.split(delimiter); 

/* print substrings */ 
for (int i = 0; i < tempArray.length; i++) 
              System.out.println(tempArray[i]); 

Output: 
java
 string split
 best practices

 

Split a string using multiple delimiters in Java

Sometimes we have String containing words separated by different characters. So how to split a String by multiple delimiters?

It is possible to use the OR symbol to separate the delimiters (In that case you need to use the double-escape characters for some special characters).

/* String to split. */ 
String stringToSplit = "java,best*practices_split@string/regexp"; 
String[] tempArray; 

/* delimiter */ 
String delimiter = "@|_|\\*|/|,"; 

/* given string will be split by the argument delimiter provided. */ 
tempArray = stringToSplit.split(delimiter); 

/* print substrings */ 
for (int i = 0; i < tempArray.length; i++) 
         System.out.println(tempArray[i]); 

Output: 
java 
best 
practices 
split 
string 
regexp

 

how to split string with multiple special characters in java

In case you use a character class[], then there is no need to use the double-escape characters.

/* String to split. */ 
String stringToSplit = "java,best*practices_split@string/regexp"; 
String[] tempArray; 

/* delimiter */ 
String delimiter = "[@_*/,]"; 

/* given string will be split by the argument delimiter provided. */ 
tempArray = stringToSplit.split(delimiter); 

/* print substrings */ 
for (int i = 0; i < tempArray.length; i++) 
               System.out.println(tempArray[i]); 

Output: 
java 
best 
practices 
split 
string 
regexp

 

How to split a string by position in java

To split a String using by a specific position you need to use substring() method

/* String to split. */ 
String stringToSplit = "java best practices"; 

/* position */ 
int position= 9; 

/* given string will be split using position provided. */ 
String first = stringToSplit.substring(0, position); 
String second= stringToSplit.substring( position); 

/* print substrings */ 
System.out.println(first ); 
System.out.println(second ); 

Output: 
java best
 practices

 

How to split a string into an array of characters

Use toCharArray() method. It splits the string into an array of characters:

/* String to split. */ 
String stringToSplit = "java best practices"; 

/* given string will be split into an array of chars */ 
char[] chars= stringToSplit.toCharArray(); 
for (char c: chars) 
       System.out.print(c+"|"); 

Output: j|a|v|a| |b|e|s|t| |p|r|a|c|t|i|c|e|s|

 

Recent Posts