Java String split Example
In this Java String split example we are going to see how to split aString in Java into smaller sub strings. It is extremely common to want to split aString into parts. These parts are separated by a specific delimiter. And in order to avoid parsing theString yourself, Java offerssplit API method.
1. Using split
It is very easy to split aString in Java usingsplit. It works like this : you give it a delimiter as argument, and it returns the sub parts of theString that are separated with the specified delimiter into an String array. The delimiter can be very simple, like a single character, or very complex like a regular expression. In fact this is the complete signature of the method :public String[] split(String regex)
Ok let’s see how you can use it:
StringSplitExample.java
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | packagecom.javacodegeeks.core.string;importjava.util.Arrays;publicclassStringSplitExample { publicstaticvoidmain(String[] args) { String str ="abdc:psdv:sdvosdv:dfpbkdd"; // split the array using ':' as a delimiter String[] parts = str.split(":"); System.out.println("Using : as a delimiter "+Arrays.toString(parts)); // split the array using 'd' as a delimiter parts = str.split("d"); System.out.println(Arrays.toString(parts)); String str2 ="This is a string to tokenize"; // tokenize the string into words simply by splitting with " " parts = str2.split(" "); System.out.println(Arrays.toString(parts)); }} |
This will be the output:
[abdc, psdv, sdvosdv, dfpbkdd][ab, c:ps, v:s, vos, v:, fpbk][This, is, a, string, to, tokenize
Some important things to note:
- The array, returned by
split, contains all sub strings of the originalStringthat end with the specified delimiter. - The sub strings in the array appear in the same order as in the original
String. - If none of the characters match the delimiter the array will have only one
String, the original one.
2. Using split with limit
You can also usepublic String[] split(String regex, int limit), to set a limit on how many times you want the string to be split. Essentially, theString will be scanned sequentially from left to right, and spit the String as it normally would, but it will stop as soon as it performslimit splits.
Take a look at this example:
StringSplitExample.java
package com.javacodegeeks.core.string;import java.util.Arrays; public class StringSplitExample { public static void main(String[] args) { String str = "abdc:psdv:sdvosdv:dfpbkdd"; String[] part0Limits = str.split(":",0); System.out.println("Using : as a delimiter with limit 0 " +Arrays.toString(part0Limits)); String[] part1Limits = str.split(":",2); System.out.println("Using : as a delimiter with limit 1 " +Arrays.toString(part1Limits)); String[] part5Limits = str.split(":",5); System.out.println("Using : as a delimiter with limit 5 " +Arrays.toString(part5Limits)); String[] partNegativeLimits = str.split(":",-2); System.out.println("Using : as a delimiter with negative limit " +Arrays.toString(partNegativeLimits)); }}This will be the output:
Using : as a delimiter with limit 0 [abdc, psdv, sdvosdv, dfpbkdd]Using : as a delimiter with limit 1 [abdc, psdv:sdvosdv:dfpbkdd]Using : as a delimiter with limit 5 [abdc, psdv, sdvosdv, dfpbkdd]Using : as a delimiter with negative limit [abdc, psdv, sdvosdv, dfpbkdd]
- The array, returned by
split(), contains all substrings of the originalStringthat end with the specified delimiter(:) If you specified the limit is 2, it will return only the two substrings. - If the limit is negative -2, then the method returns the substring with no limit.
- If the limit is 0, then the method returns all substring excluding the trailing empty string.
3. Using split with regular expressions
Some times it is very convenient to use a regular expression instead of a delimiter to split aString. For example, you may want to split aString using any number as a delimiter, and not just a constant character.
Let’s see how you can do that:
StringSplitExample.java
package com.javacodegeeks.core.string;import java.util.Arrays;public class StringSplitExample {public static void main(String[] args) {String str = "abdc124psdv456sdvos456dv568dfpbk0dd";// split the array using a single digit, e.g 1,2,3...String[] parts = str.split("[0-9]");System.out.println(Arrays.toString(parts));// split the array using a whole number, e.g 12,346,756parts = str.split("[0-9]+");System.out.println(Arrays.toString(parts));//split the string using series of operatorsSystem.out.println("Split string using operators :");String input = "test,java,code-geek@java8?split.example";String[] strOutput = input.split("[,-?.@]+");for (String s : strOutput)System.out.println(s);// split the string using ^System.out.println("Split string using ^ :");String input2 = "Java^CodeGeeks";String strOutput2[] = input2.split("\\^");for (String s : strOutput2)System.out.println(s);}}This will be the output:
[abdc, , , psdv, , , sdvos, , , dv, , , dfpbk, dd][abdc, psdv, sdvos, dv, dfpbk, dd]Split string using operators :testjavacodegeekjavasplitexampleSplit string using ^ :JavaCodeGeeks
The array, returned bysplit(), contains all substrings, which is split using expressions using operators or numbers.
4. Download the source code
This was an example on Java String split.
You can download the source code of this example here :Java String split Example
Last updated on Mar. 23rd, 2020

Thank you!
We will contact you soon.



