- Different Ways to Split a String in Java
- How to Split a String in Java
- Spliting a String in Java
- Result
- 1. Using String.split ()
- Example
- Example
- Result
- Example
- Result
- Result
- Result
- 2. Using StringTokenizer
- Example
- Result
- Example
- Result
- 3. Using Pattern.compile ()
- Example
- Result
- Java String split() Method with examples
- Java String Split Method
- String split() method Example
- Java String split() method with multiple delimiters
- Java Split String Examples
- Example 1: Split string using word as delimiter
- Example 2: Split string by space
- Example 3: Split string by pipe
- Example 4: Split string by dot ( . )
- Example 5: Split string into array of characters
- Example 6: Split string by capital letters
- Example 7: Split string by newline
- Example 8: Split string by comma
- Top Related Articles:
- About the Author
- Comments
Different Ways to Split a String in Java
Learn to split or tokenize a string into an array. Splitting a string is a very common task, especially working on web applications when we have to pass data in CSV format or separate based on some other separator such $ , # or another separator.
The String.split() method is the best and recommended way to split the strings. The tokens are returned in form of a string array that frees us to use it as we wish.
The following Java program splits a string with the delimiter comma. It is equivalent to splitting a CSV file.
String blogName = "how,to,do,in,java"; String[] tokenArray = blogName.split(","); //["how", "to", "do", "in", "java"]
We need to modify the regex expression for any additional requirements. Such for ignoring the whitespaces around commas, we can use the pattern “\\s,\\s”.
String[] tokenArray = blogName.split("\\s*,\\s*");
The String.split() is very straightforward simple API for simple usages. If we want to process the tokens post splitting but before concluding the final result, the Splitter class is the best.
- Using Splitter makes the code more readable and reusable also. We create a Splitter instance and reuse it multiple times, thus helping achieve uniform logic splitting in the whole application.
- Another benefit is that it also provided some useful methods while building the splitter itself, eliminating a lot of after-work after creating the tokens.
We can directly include Guava from the maven repository.
We can create a Splitter instance in a fluent manner:
Splitter niceCommaSplitter = Splitter.on(',').omitEmptyStrings().trimResults();
And now, use it anywhere in the code as we like. Note that we have the commas twice. The Splitter handles it well and does not include the empty token.
Splitter niceCommaSplitter = Splitter.on(',').omitEmptyStrings().trimResults(); Iterable tokensList = niceCommaSplitter.split("how,to,do,in, ,java"); tokensList.forEach(System.out::println); //"how", "to", "do", "in", "java"
The Apache Commons Lang’s StringUtils class provides many useful methods to perform common operations on Strings, such as search, replace, reverse or check empty. All operations are null safe.
The StringUtils.split() is very similar to the above approach and also returns the String array output. The only benefit is that the code is faster.
Start with including the latest version of common-lang3 dependency.
org.apache.commons commons-lang3 3.12.0
The constructor to initialize the StringUtils takes the following parameters:
split(String str, String separatorChars, int max)
- str – the String to parse, may be null.
- separatorChars (Optional) – the characters used as the delimiters. The default value is whitespace.
- max (Optional) – the maximum number of elements to include in the array. A zero or negative value implies no limit.
The following Java program using StringUtils splits a string by delimiter whitespace.
String[] tokens = StringUtils.split("how to do in java"); Assertions.assertArrayEquals(new String[], tokens);
How to Split a String in Java
Sometimes we need to split a string in programming. We suggest String.split (), StringTokenizer, and Pattern.compile () methods.
Spliting a String in Java
public class StringSplitTest < public static void main(String[] arg) < String str = "Welcome:dear guest"; String[] arrOfStr = str.split(":"); for (String a: arrOfStr) System.out.println(a); > >
The output for this will be like this:
Result
Note: Change regex to come to a different result:
E.g. for («e») regex the result will we like this:
There are many ways to split a string in Java. The most common way is using the split() method which is used to split a string into an array of sub-strings and returns the new array.
1. Using String.split ()
The string split() method breaks a given string around matches of the given regular expression. There are two variants of split() method in Java:
This method takes a regular expression as a parameter and breaks the given string around matches of this regular expression regex. By default limit is 0.
Parameter for this is: regex (a delimiting regular expression).
It returns an array of strings calculated by splitting the given string.
Example
Parameters for this are: regex (the delimiting regular expression) and limit (controls the number of times the pattern is applied and therefore affects the length of the resulting array).
This returns the array of strings counted by splitting this string around matches of the given regular expression.
Example
public class StringSplitTest < public static void main(String[] arg) < String str = "Hello:world:hello"; String split[] = str.split("e", 5); for (String s: split) System.out.println(s); > >
The output for the given example will be like this:
Result
Note: Change regex and limit to have different outputs: e.g. («:», 2)-output will be ; («:», -2)-, etc.
Example
public class StringSplitTest < public static void main(String[] arg) < String str = "What are you doing today?"; String split[] = str.split(" ", 0); for (String s: split) System.out.println(s); > >
The output will be the following:
Result
Note: Change regex and limit to have another output: e.g. (» «, 2) — the result will be like this:
Result
public class StringSplitTest < public static void main(String args[]) < String s = " ;String; String; String; String, String; String;;String;String; String; String; ;String;String;String;String"; //String[] strs = s.split("[,\\s\\;]"); String[] strs = s.split("[,\\;]"); System.out.println("Substrings length:" + strs.length); for (int i = 0; i < strs.length; i++) < System.out.println("Str[" + i + "]:" + strs[i]); > > >
Output for this will be like this:
Result
Substrings length:17 Str[0]: Str[1]:String Str[2]: String Str[3]: String Str[4]: String Str[5]: String Str[6]: String Str[7]: Str[8]:String Str[9]:String Str[10]: String Str[11]: String Str[12]: Str[13]:String Str[14]:String Str[15]:String Str[16]:String
2. Using StringTokenizer
In Java, the string tokenizer allows breaking a string into tokens. You can also get the number of tokens inside string object.
Example
import java.util.StringTokenizer; public class StringTokenizerExample < public static void main(String[] args) < StringTokenizer st = new StringTokenizer("A StringTokenizer sample"); // get how many tokens are inside st object System.out.println("Tokens count: " + st.countTokens()); // iterate st object to get more tokens from it while (st.hasMoreElements()) < String token = st.nextElement().toString(); System.out.println("Token = " + token); > > >
Result
Tokens count: 3 Token = A Token = StringTokenizer Token = sample
Note: You can specify the delimiter that is used to split the string. In the above example we have set the delimiter as space (”).
It is also possible to use StringTokenizer with multiple delimiters.
Example
import java.util.StringTokenizer; public class StringTokenizerExample < public static void main(String[] args) < String url = "http://www.w3docs.com/learn-javascript.html"; StringTokenizer multiTokenizer = new StringTokenizer(url, "://.-"); while (multiTokenizer.hasMoreTokens()) < System.out.println(multiTokenizer.nextToken()); >> >
Result
http www w3docs com learn javascript html
Note: In the above-mentioned example :, //, ., — delimiters are used.
3. Using Pattern.compile ()
This method splits the given input sequence around matches of the pattern. Parameter for this is: input — the character sequence to be split.
It returns the array of strings computed by splitting the input around matches of the pattern.
Example
import java.util.regex.Pattern; public class PatternDemo < private static String REGEX = ":"; private static String INPUT = "hello:dear:guest"; public static void main(String[] args) < Pattern pattern = Pattern.compile(REGEX); String[] result = pattern.split(INPUT); for (String data: result) < System.out.println(data); >> >
This will produce the following result:
Result
Java String split() Method with examples
Java String split method is used for splitting a String into substrings based on the given delimiter or regular expression.
For example:
Input String: [email protected] Regular Expression: @ Output Substrings:
Java String Split Method
We have two variants of split() method in String class.
1. String[] split(String regex) : It returns an array of strings after splitting an input String based on the delimiting regular expression.
2. String[] split(String regex, int limit) : This method is used when you want to limit the number of substrings. The only difference between this variant and above variant is that it limits the number of strings returned after split up. For example: split(«anydelimiter», 3) would return the array of only 3 strings even if there can be more than three substrings.
What if limit is entered as a negative number?
If the limit is negative then the returned string array would contain all the substrings including the trailing empty strings, however if the limit is zero then the returned string array would contains all the substrings excluding the trailing empty Strings.
It throws PatternSyntaxException if the syntax of specified regular expression is not valid.
String split() method Example
If the limit is not defined:
If positive limit is specified in the split() method: Here the limit is specified as 2 so the split method returns only two substrings.
If limit is specified as a negative number: As you can see that when the limit is negative, it included the trailing empty strings in the output. See the output screenshot below.
Limit is set to zero: This will exclude the trailing empty strings.
Difference between zero and negative limit in split() method:
- If the limit in split() is set to zero, it outputs all the substrings but exclude trailing empty strings if present.
- If the limit in split() is set to a negative number, it outputs all the substrings including the trailing empty strings if present.
Java String split() method with multiple delimiters
Let’s see how we can pass multiple delimiters while using split() method. In this example we are splitting input string based on multiple special characters.
Number of substrings: 7 Str[0]: Str[1]:ab Str[2]:gh Str[3]:bc Str[4]:pq Str[5]:kk Str[6]:bb
Lets practice few more examples:
Java Split String Examples
Example 1: Split string using word as delimiter
Here, a string (a word) is used as a delimiter in split() method.
Example 2: Split string by space
String[] strArray = str.split("\\s+");
Input: "Text with spaces"; Output: ["Text", "with", "spaces"]
Example 3: Split string by pipe
String[] strArray = str.split("\\|");
Input: "Text1|Text2|Text3"; Output: ["Text1", "Text2", "Text3"]
Example 4: Split string by dot ( . )
String[] strArray = str.split("\\.");
You can split string by dot ( . ) using \\. regex in split method.
Input: "Just.a.Simple.String"; Output: ["Just", "a", "Simple", "String"]
Example 5: Split string into array of characters
String[] strArray = str.split("(?!^)");
The ?! part in this regex is negative assertion, which it works like a not operator in the context of regular expression. The ^ is to match the beginning of the string. Together it matches any character that is not the beginning of the string, which means it splits the string on every character.
Input: "String"; Output: ["S", "t", "r", "i", "n", "g"]
Example 6: Split string by capital letters
String[] strArray = str.split("(?=\\p)");
\p is a shorthand for \p . This regex matches uppercase letter. The extra backslash is to escape the sequence. This regex split string by capital letters.
Input: "BeginnersBook.com"; Output: ["Beginners", "Book.com"]
Example 7: Split string by newline
String[] str = str.split(System.lineSeparator());
This is one of the best way to split string by newline as this is a system independent approach. The lineSeparator() method returns the character sequence for the underlying system.
Example 8: Split string by comma
String[] strArray = str.split(",");
To split the string by comma, you can pass , special character in the split() method as shown above.
Top Related Articles:
About the Author
I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.
Comments
Is it right to say that 28 is present at array1[0], 12 at array1[1] and 2013 at array1[2]?
I am really confused right now.Please help.
It would be helpful to include some examples that require use of the escape characters and which characters need them. It is one thing I was looking for. Once I realized that “|” needed “\\|”, my split worked like a champ. Thanks for showing these using small code bits. It really does make a difference.