Java split one word

Use the appropriately named method String#split().

String string = "004-034556";
String[] parts = string.split("-");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556

Note that split‘s argument is assumed to be a regular expression, so remember to escape special characters if necessary.

there are 12 characters with special meanings: the backslash , the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), and the opening square bracket [, the opening curly brace {, These special characters are often called «metacharacters».

For instance, to split on a period/dot . (which means «any character» in regex), use either backslash to escape the individual special character like so split("\."), or use character class [] to represent literal character(s) like so split("[.]"), or use Pattern#quote() to escape the entire string like so split(Pattern.quote(".")).

String[] parts = string.split(Pattern.quote(".")); // Split on the exact string.

To test beforehand if the string contains certain character(s), just use String#contains().

if (string.contains("-")) {
    // Split it.
} else {
    throw new IllegalArgumentException("String " + string + " does not contain -");
}

Note, this does not take a regular expression. For that, use String#matches() instead.

If you’d like to retain the split character in the resulting parts, then make use of positive lookaround. In case you want to have the split character to end up in left hand side, use positive lookbehind by prefixing ?<= group on the pattern.

String string = "004-034556";
String[] parts = string.split("(?<=-)");
String part1 = parts[0]; // 004-
String part2 = parts[1]; // 034556

In case you want to have the split character to end up in right hand side, use positive lookahead by prefixing ?= group on the pattern.

String string = "004-034556";
String[] parts = string.split("(?=-)");
String part1 = parts[0]; // 004
String part2 = parts[1]; // -034556

If you’d like to limit the number of resulting parts, then you can supply the desired number as 2nd argument of split() method.

String string = "004-034556-42";
String[] parts = string.split("-", 2);
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556-42

The string split() method breaks a given string around matches of the given regular expression. After splitting against the given regular expression, this method returns a string array.

Input String: 016-78967
Regular Expression: - 
Output : {"016", "78967"}

Following are the two variants of the split() method in Java: 

1. Public String [] split ( String regex, int limit)

Parameters:

  • regex – a delimiting regular expression
  • Limit – the resulting threshold

Returns: An array of strings is computed by splitting the given string.

Exception Thrown: PatternSyntaxException – if the provided regular expression’s syntax is invalid.  

The limit parameter can have 3 values: 

  • limit > 0 – If this is the case, then the pattern will be applied at most limit-1 times, the resulting array’s length will not be more than n, and the resulting array’s last entry will contain all input beyond the last matched pattern.
  • limit < 0 – In this case, the pattern will be applied as many times as possible, and the resulting array can be of any size.
  • limit = 0 – In this case, the pattern will be applied as many times as possible, the resulting array can be of any size, and trailing empty strings will be discarded.

Here’s how it works:

 Let the string that is to be split is – geekss@for@geekss

Regex   Limit   Result
@ 2 {“geekss”, ”for@geekss”}
@ 5 {“geekss”, ”for”, ”geekss”} 
@ -2 {“geekss”, ”for”, ”geekss”}
s     5 {“geek”, ”“, “@for@geek”, “”, “”}
s     -2 {“geek”, ” “, ” “, “@for@geek”, “”, “”}
s     0 {“geek”, ””, ”@for@geek”}

Following are the Java example codes to demonstrate the working of split()

 Example 1:

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "geekss@for@geekss";

        String[] arrOfStr = str.split("@", 2);

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

Example 2:  

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "geekss@for@geekss";

        String[] arrOfStr = str.split("@", 5);

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

Example 3:  

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "geekss@for@geekss";

        String[] arrOfStr = str.split("@", -2);

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

Example 4:  

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "geekss@for@geekss";

        String[] arrOfStr = str.split("s", 5);

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

Example 5:  

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "geekss@for@geekss";

        String[] arrOfStr = str.split("s", -2);

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

Example 6:  

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "geekss@for@geekss";

        String[] arrOfStr = str.split("s", 0);

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

2. public String[] split(String regex)

This variant of the split method takes a regular expression as a parameter and breaks the given string around matches of this regular expression regex. Here, by default limit is 0.
 

Parameters: regex – a delimiting regular expression

Returns: An array of strings is computed by splitting the given string.

Exception Thrown: PatternSyntaxException – if the provided regular expression’s syntax is invalid.  

Here are some working example codes:

 Example 1:

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "GeeksforGeeks:A Computer Science Portal";

        String[] arrOfStr = str.split(":");

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

Output

GeeksforGeeks
A Computer Science Portal

Example 2:

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "GeeksforGeeksforStudents";

        String[] arrOfStr = str.split("for");

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

Output

Geeks
Geeks
Students

It can be seen in the above example that the pattern/regular expression “for” is applied twice (because “for” is present two times in the string to be split)

Example 3:  

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "Geeks for Geeks";

        String[] arrOfStr = str.split(" ");

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

Example 4:

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "Geeks.for.Geeks";

        String[] arrOfStr = str.split("[.]");  

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

 Example 5: 

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "Geekssss";

        String[] arrOfStr = str.split("s");

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

In the above example that trailing empty strings are not included in the resulting array arrOfStr.

  Example 6:

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "GeeksforforGeeksfor   ";

        String[] arrOfStr = str.split("for");

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

In the above example, the trailing spaces (hence not empty string) become a string in the resulting array arrOfStr.

 Example 7:

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "word1, word2 word3@word4?word5.word6";

        String[] arrOfStr = str.split("[, ?.@]+");

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

Output

word1
word2
word3
word4
word5
word6

In the above example, words are separated whenever either of the characters specified in the set is encountered.

This article is contributed by Vaibhav Bajpai. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect or if you want to share more information about the topic discussed above.

Introduction

Oftentimes, we are faced with a situation where we need to split a string at some specific character or substring, to derive some useful information from it.

For example, we might want to split a phone number on the country code or data imported from a CSV file.

In this article, we’ll cover how to split a String in Java.

The split() Method (Without a Limit)

This method takes one String parameter, in regular expression (regex) format. This method splits the string around the matches of the given regular expression.

The syntax for this method is:

String[] split(String regex, int limit)

Where the regex parameter represents the delimiter, i.e. based on what we’ll split our string. Keep in mind that this parameter doesn’t need to be anything complicated, Java simply provides the option of using regular expressions.

For example, let’s see how we can split this String into two separate names:

String myString = "Jane-Doe";
String[] splitString = myString.split("-");

We can simply use a character/substring instead of an actual regular expression. Of course, there are certain special characters in regex which we need to keep in mind, and escape them in case we want their literal value.

Once the string is split, the result is returned as an array of Strings. Strings in the returned array appear in the same order as in the original string.

The results are packed in the String array. To retrieve the separate names, we can access each element:

System.out.println(splitString[0]);
System.out.println(splitString[1]);

This results in:

Jane
Doe

Keep in mind, this method will split the string on all occurrences of the delimiter. For example, we can have a CSV formatted input:

String myString = "Jane,21,Employed,Software Engineer";
String[] splitString = myString.split(",");

for (String s : splitString) {
    System.out.println(s);
}

This results in:

Jane
21
Employed
Software Engineer

Java split() Method (With a Limit)

Here, the method takes on two parameters, one being the previously discussed regex, and the other being an integer value, denoting the limit. The limit parameter is used to decide how many times we want to split the string.

The limit parameter can take one of three forms, i.e it can either be greater than, less than or above zero. Let’s take a look at what each of these situations represents:

  • A positive limit — The String will be split up to a maximum of limit - 1 times. Beyond this, the rest of the string will be returned as the last element of the array, as it is, without splitting. The length of the returned array will always be less than or equal to limit.
  • A negative limit — The String is split at the delimiter as many times as possible, ignoring the particular negative value set. The substrings in the array include the trailing spaces in the original string, if there are any.
  • When the limit is set to 0 — The String is again split as many times as possible, and there is no limit on the length of the resulting array. The works the same as calling the split() method, with regex as the only argument, as seen earlier. In this case, trailing spaces are not returned.

Positive Limit Value

Let’s take a look at some examples of using different limits. Firstly, a positive limit value:

String myString = "there,,are,more,than,three,commas,,,";
String [] splitStrings = myString.split(",", 4);

for(String string : splitStrings){
    System.out.println(String.format(" " %s "", string));
}

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

With a limit of 4, the String will be split at most three (limit - 1) times. Which gives us an array with four elements (0..3), the last element being everything after the third split:

"there"
""
"are"
"more,than,three,commas,,,"

If we used a negative limit on this same String:

String myString = "there,,are,more,than,three,commas,,,";
String [] splitStrings = myString.split(",", -1);

for(String string : splitStrings){
    System.out.println(String.format(" " %s "", string));
}

The String will be split as many times as possible, and the trailing empty strings would be added to the array:

"there"
""
"are"
"more"
"than"
"three"
"commas"
""
""
""

The actual negative value we used isn’t taken into consideration, we would get the same result if we used -150.

If we set the limit to 0, the String would again be split as many times as possible, but the resulting array wouldn’t contain the trailing empty spaces:

String myString = "there,,are,more,than,three,commas,,,";

// Equivalent to calling the split() method with only the regex parameter
String [] splitStrings = myString.split(",", 0);

for(String string : splitStrings){
    System.out.println(String.format(" " %s "", string));
}

This would give us:

"there"
""
"are"
"more"
"than"
"three"
"commas"

Note on Special Characters

As we mentioned earlier, the regex parameter passed as the delimiter in the split() method is a regular expression. We have to make sure to escape special characters if we want to use their literal value as a delimiter. For example, the * character means «one or more instances of the following character(s)».

There are 12 such characters in regex. These are: , ^, $, ., |, ?, *, +, (, ), [, {. You can see their meaning in regex here.

If we want to split a String at one of these characters, special care has to be taken to escape these characters in the method parameters. One way we can use this is to use a backslash . For example:

string.split("\|");

Splits the string variable at the | character. We use two backlashes here since we need to first escape the Java-meaning of the backlash, so the backslash can be applied to the | character.

Instead of this, we can use a regex character set This refers to putting the special characters to be escaped inside square brackets. This way, the special characters are treated as normal characters. For example, we could use a | as a delimiter by saying:

string.split("[|]");

Yet another way to escape special characters is to use Pattern.quote():

string.split(Pattern.quote("|"));

Conclusion

The split() method of the Java String class is a very useful and often used tool. Most data, especially that obtained from reading files would require some amount of pre-processing, such as splitting the string, to obtain meaningful information from it.

In this article, we’ve gone over how to split strings in Java.

1. Overview

As part of
String API features series, You’ll learn
how to split a string in java and
also
how to convert a String into String array for a given delimiter

This concept looks easy but quite a little tricky. You’ll understand this
concept clearly after seeing all the examples on the split() method.

The string split() method breaks a given string around matches of the
given regular expression.

Java String split() Examples on How To Split a String

2. split() Syntax

The string class has two versions of the split() method. The first one takes
an only regular expression. The second version takes two args with reg
expression and limit is to limit the number of splits.

public String[] split​(String regex)

public String[] split​(String regex, int limit)

Parameters:

regex — the delimiting regular
expression

limit — the result threshold

Returns always a String array.

3. public String[] split(String regex) Examples

In the below examples, you will see the different behavior with split(String
regex) method

3.1 Example 1- How to split based on Special character ‘@’

Splitting based on ‘@’ delimiter and exactly divides into how many times
appears in the input string.

package com.javaprogramto.strings.split;

public class StringSplitExample1 {

    public static void main(String[] args) {

        String str = "java@program@to.com";

        String[] splitArray = str.split("@");

        for(String value : splitArray){

            System.out.println(value);

        }

    }

}

Output:

3.2 Example 2 — How to Split based on another String

Splitting based on the string «for».

public class StringSplitExample2 {

    public static void main(String[] args) {

        String str = "javaprogramto.comisfordevelopersandforfreshers";

        String[] splitArray = str.split("for");

        for(String value : splitArray){

            System.out.println(value);

        }

    }

}

Output:

Word «for» is appeared twice so the string is divided into 3 strings.

javaprogramto.comis

developersand

freshers

3.3 Example 3 — How to Split based on the empty space

package com.javaprogramto.strings.split;

public class StringSplitExample3 {

    public static void main(String[] args) {

        String str = "java programs for freshers";

        String[] spaceBasedSplitArray = str.split(" ");

        for(String value : spaceBasedSplitArray){

            System.out.println(value);

        }

    }

}

Output:

3.4 Example 4 — How to Split phone number based on delimiter ‘-‘

package com.javaprogramto.strings.split;

public class StringSplitExample4 {

    public static void main(String[] args) {

        String phoneNo = "123-567-9080";

        String[] spaceBasedSplitArray = phoneNo.split("-");

        for(String value : spaceBasedSplitArray){

            System.out.println(value);

        }

    }

}

Output:

3.5 Example 5 — How to split Repeat Delimiter at end of string

If the delimiter is repeated at the end of the string then all trailing empty
strings will be ignored. So returned String array will not have the empty
spaces.

package com.javaprogramto.strings.split;

public class StringSplitExample5 {

    public static void main(String[] args) {

        String delimitetAtEndString = "1235-567-9080-1234-----";

        String[] spaceBasedSplitArray = delimitetAtEndString.split("-");

        for(String value : spaceBasedSplitArray){

            System.out.println(value);

        }

    }

}

Output:


3.6 Example 6 — How to split Repeat Delimiter in middle of the string

If the delimiter appears in the middle of String or in between start and end
index then it considers the blank spaces.

package com.javaprogramto.strings.split;

public class StringSplitExample6 {

    public static void main(String[] args) {

        String delimitetAtEndString = "1235--------89";

        String[] spaceBasedSplitArray = delimitetAtEndString.split("-");

        System.out.println("returned array size : "+spaceBasedSplitArray.length);

        for(String value : spaceBasedSplitArray){

            System.out.println(value);

        }

    }

}

Output:

returned array size : 9

1235

89

3.7 Example 7 — How to split Multiple Delimiters Using Regular Expression

Next, Look at the input string which is «hello,welcome to;java$program#to.com» is having many delimiters such as comma(,), white space, ;, $ and # symbols.

Below programs, show how to split based on assorted types of delimiters. You
need to pass all of these characters inside square brackets [] then all are
considered as a regular expression.

package com.javaprogramto.strings.split;

public class StringSplitExample7 {

    public static void main(String[] args) {

        String multipleDelimiters = "hello,welcome to;java$program#to.com";

        String[] spaceBasedSplitArray = multipleDelimiters.split("[, ;$#]");

        System.out.println("returned array size : "+spaceBasedSplitArray.length);

        for(String value : spaceBasedSplitArray){

            System.out.println(value);

        }

    }

}

Output:

Observe the output and split with all special characters whatever is included
in the pattern.

returned array size : 6

hello

welcome

to

java

program

to.com

4. public String[] split(String regex, int limit) Examples

Furthermore on this area, split(String regex, int limit) method works
differently.

But, you need to understand that normal split(String regex) method internally
calls the limit argument method split(String regex, int limit).

Limit parameter value plays an important role in this method and
controls the number of times the pattern is applied and therefore affects
the length of the resulting array.

If the limit is positive then the pattern will be applied at most limit — 1
times, the array’s length will be no greater than the limit, and the array’s
last entry will contain all input beyond the last matched delimiter.

If the limit is zero then the pattern will be applied as many times as
possible, the array can have any length, and trailing empty strings will be
discarded.

If the limit is negative then the pattern will be applied as many times as
possible and the array can have any length.

Now, it is time to see the more examples with limit option and understand
in-depth.

4.1 Example 8 — split with limit value 0

If the limit value is passed as 0 then it works similar to the regular
split(regex) method.

  

package com.javaprogramto.strings.split;

public class StringSplitLimitExample1 {

    public static void main(String[] args) {

        String withLimit0 = "Java:Program:to:.com";

        String[] spaceBasedSplitArray = withLimit0.split(":", 0);

        System.out.println("returned array size : "+spaceBasedSplitArray.length);

        for(String value : spaceBasedSplitArray){             System.out.println(value);

        }

    }

}

Output:

This produced the output as same as the regular method.

returned array size : 4

Java

Program

to

.com

4.2 Example 9 — split with limit value 1

Now, Understand how to split() method works with the different limit
values. Pass the limit value as 1 and see the output.

package com.javaprogramto.strings.split;

public class StringSplitLimitExample1 {

    public static void main(String[] args) {

        String withLimit1 = "Java:Program:to:.com";

        String[] spaceBasedSplitArray = withLimit1.split(":", 1);

        System.out.println("returned array size with limit 1 : "+spaceBasedSplitArray.length);

        for(String value : spaceBasedSplitArray){

            System.out.println(value);

        }

    }

}

Output:

returned array size with limit 1 : 1

Java:Program:to:.com

The above program produced the output without splitting. It just printed the
only string and did not split based on the given delimiter colon(:) because we
passed the limit value as 1. That indicates to the program to return string
array should have only one value.

Look at the next example with a limit value 2.

4.3 Example 10 — split with limit value 2

package com.javaprogramto.strings.split;

public class StringSplitLimitExample1 {

    public static void main(String[] args) {

        String withLimit1 = "Java:Program:to:.com";

        String[] spaceBasedSplitArray = withLimit1.split(":", 1);

        System.out.println("returned array size with limit 1 : "+spaceBasedSplitArray.length);

        for(String value : spaceBasedSplitArray){

            System.out.println(value);

        }

    }

}

Output:

  

returned array size with limit 2 : 2

Java

Program:to:.com

In this program, we passed the limit value 2 to the split() method to produce
the String array with two values. Even though the delimiter is present more
than 2 times but it returns an array with two values only.

4.4 Example  11 —  String ends with a delimiter and with the limit
value


If the string is ending with delimiters and all of these are discarded when
split(regex) method is executed. But if you want to get those values also
then need to pass the limit value.

See the below program with and without limit value. split(limit) method call
returned string will have all blank values.

package com.javaprogramto.strings.split;

public class StringSplitLimitExample1 {

    public static void main(String[] args) {
        String stringWithEndingWithDelimiter = "Java:Program:to:.com:::";

        String[] arrayWithoutLimitValue = stringWithEndingWithDelimiter.split(":");

        System.out.println("returned array size without limit parameter : "+arrayWithoutLimitValue.length);
        for(String value : arrayWithoutLimitValue){
            System.out.println(value);
        }

        String[] arrayWithLimitValue = stringWithEndingWithDelimiter.split(":", 7);

        System.out.println("returned array size with limit parameter : "+arrayWithLimitValue.length);
        for(String value : arrayWithLimitValue){
            System.out.println(value);
        }
        System.out.println("Done");
    }
}

Output:

returned array size without limit parameter : 4
Java
Program
to
.com
returned array size with limit parameter : 7
Java
Program
to
.com



Done

4.5 Example  12 — Delimiter in the middle and end of the string (Negative
limit value)

If the delimiter appears in middle and at the end of the string then the
trailing values will be omitted with the split(regex) method.

In most of the cases, we do not know how many times delimiter will be at the
end of the string. In this case, we need to pass the right value to the limit
parameter then only it gets all values.

But, it is impossible to get the number of occurrences of a delimiter at the
end of the string. So, handle such situations pass the negative index that
holds all trailing values.

package com.javaprogramto.strings.split;

public class StringSplitLimitExample3 {

    public static void main(String[] args) {
        String stringWithEndingWithDelimiter = "Java:Program:::to:.com:::";

        String[] arrayWithoutLimitValue = stringWithEndingWithDelimiter.split(":");

        String[] arrayWithLimitValue = stringWithEndingWithDelimiter.split(":", -2);

        System.out.println("returned array size with limit parameter : "+arrayWithLimitValue.length);
        for(String value : arrayWithLimitValue){
            System.out.println(value);
        }
        System.out.println("Done");
    }
}

Output:

5. Validation with split() method

If you are expecting a particular number of values in a string then you can
use the split() method.

String[] result = yourString.split("-");

if (result.length != 2) 

     throw new IllegalArgumentException("String not in correct format");

This will split your string into 2 parts. The first element in the array
will be the part containing the stuff before the -, and the 2nd element in
the array will contain the part of your string after the -.

If the array length is not 2, then the string was not in the format:
string-string. So, it throws IllegalArgumentException.

6. How to split string in java with dot in two ways

Example 13: Way 1 with dot

If you use directly as split(«.»), it will not work. Dot is a special
character and so should use with double backslash’s.

package com.javaprogramto.strings.split;

public class StringSplitExampleWithDot {

    public static void main(String[] args) {
        String multipleDelimiters = "hello.world.learn.java";

        String[] spaceBasedSplitArray = multipleDelimiters.split("\.");

        System.out.println("Splitting string with dot way 1");
        for (String value : spaceBasedSplitArray) {
            System.out.println(value);
        }
    }
}

Output:

Splitting string with dot 
hello
world
learn
java

Example 14: Way 2 with dot using regular expression

package com.javaprogramto.strings.split;

public class StringSplitExampleWithDot {

    public static void main(String[] args) {
        String multipleDelimiters = "hello.world.learn.java";

        String[] spaceBasedSplitArray = multipleDelimiters.split("[.]");

        System.out.println("Splitting string with dot using regex 1");
        for (String value : spaceBasedSplitArray) {
            System.out.println(value);
        }

        multipleDelimiters = "welcome.to.the.javaprogramto.com";

        spaceBasedSplitArray = multipleDelimiters.split("[.]");

        System.out.println("nSplitting string with dot using regex 2 n");
        for (String value : spaceBasedSplitArray) {
            System.out.println(value);
        }


    }
}

Output:

Splitting string with dot using regex 1
hello
world
learn
java

Splitting string with dot using regex 2 

welcome
to
the
javaprogramto
com

7. How to split string in java with slash

When working with file locations you want to find the no of folder in the
path. You can use split() method to find the folders count.

package com.javaprogramto.strings.split;

public class StringSplitExampleWithSlash {

    public static void main(String[] args) {
        String fileLocation = "/Users/javaprogramto/Documents/course/blog/workspace/CoreJava/split.java";

        String[] slashBasedSplitArray = fileLocation.split("/");

        System.out.println("Splitting string with dot using regex 1");
        for (String value : slashBasedSplitArray) {
            System.out.println(value);
        }

        System.out.println("No of folders in the path : " + (slashBasedSplitArray.length - 1));


    }
}

Output:

Splitting string with dot using regex 1

Users
javaprogramto
Documents
course
blog
workspace
CoreJava
split.java
No of folders in the path : 8

8. How to split String in Java by WhiteSpace or tabs? 

You can use the whitespace regex: str = «Hello baby, welcome to sweet
world»; String[] splited = str. split(«\s+»); This will cause any number
of consecutive spaces to split your string into tokens

package com.javaprogramto.strings.split;

public class StringSplitExampleWithSpace {

    public static void main(String[] args) {
        String coutries = "TR TM TC TV UG UA AE GB UM US UY UZ VU VE";

        String[] spaceBasedSplitArray = coutries.split(" ");

        System.out.println("Splitting string with space 1");
        for (String value : spaceBasedSplitArray) {
            System.out.println(value);
        }

        coutries = "TUR TKM TCA TUV UGA UKR ARE GBR UMI USA URY UZB VUT VEN";

        spaceBasedSplitArray = coutries.split("\s+");

        System.out.println("nSplitting string with space 2");
        for (String value : spaceBasedSplitArray) {
            System.out.println(value);
        }
    }
}

Output:

Splitting string with space 1
TR
TM
TC
TV
UG
UA
AE
GB
UM
US
UY
UZ
VU
VE

Splitting string with space 2
TUR
TKM
TCA
TUV
UGA
UKR
ARE
GBR
UMI
USA
URY
UZB
VUT
VEN

9. How To Split a Java String by the pipe symbol using split(“|”)

Splitting string with pipe delimiter, you can use pattern «\|»
or Pattern.quote(«|») to string split() method.

package com.javaprogramto.strings.split;

import java.util.regex.Pattern;

public class StringSplitExampleWithPipe {

    public static void main(String[] args) {
        String countryNames = "Turkey|Turkmenistan|Turks and Caicos Islands (the)|Tuvalu|Uganda|Ukraine|United Arab Emirates (the)|United Kingdom of Great Britain and Northern Ireland (the)|United States Minor Outlying Islands (the)|United States of America (the)|Uruguay|Uzbekistan|Vanuatu|Venezuela (Bolivarian Republic of)";

        String[] pipeBasedSplitArray = countryNames.split("\|");

        System.out.println("Splitting string with pipe 1");
        for (String value : pipeBasedSplitArray) {
            System.out.println(value);
        }

        String countryCodes = "792|795|796|798|800|804|784|826|581|840|858|860|548|862";

        pipeBasedSplitArray = countryCodes.split(Pattern.quote("|"));

        System.out.println("nSplitting string with pipe 2");
        for (String value : pipeBasedSplitArray) {
            System.out.println(value);
        }
    }
}

Output:

Splitting string with pipe 1
Turkey
Turkmenistan
Turks and Caicos Islands (the)
Tuvalu
Uganda
Ukraine
United Arab Emirates (the)
United Kingdom of Great Britain and Northern Ireland (the)
United States Minor Outlying Islands (the)
United States of America (the)
Uruguay
Uzbekistan
Vanuatu
Venezuela (Bolivarian Republic of)

Splitting string with pipe 2
792
795
796
798
800
804
784
826
581
840
858
860
548
862

10. Conclusion

In this article, you’ve seen
how to split a string in java with a split() method

How the split() method works with different input values.

All are shown with
examples of programs with limit value positive, zero and negative
value
.

Examples shown are over
GitHub.

Java split String by words example shows how to split string into words in Java. The example also shows how to break string sentences into words using the split method.

The simplest way to split the string by words is by the space character as shown in the below example.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

package com.javacodeexamples.stringexamples;

import java.util.Arrays;

public class StringSplitByWords {

    public static void main(String[] args) {

        String sentence = «Java String split by words from sentence»;

        //get words from sentence

        String[] words = splitSentenceByWords(sentence);

        //print words

        System.out.println(Arrays.toString(words));

    }

    private static String[] splitSentenceByWords(String str){        

        //if string is empty or null, return empty array

        if(str == null || str.equals(«»))

            return new String[0];

        String[] words = str.split(» «);

        return words;        

    }

}

Output

[Java, String, split, by, words, from, sentence]

As you can see from the output, it worked for the test sentence string. The sentence is broken down into words by splitting it using space.

Let’s try some other not-so-simple sentences.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

package com.javacodeexamples.stringexamples;

import java.util.Arrays;

public class StringSplitByWords {

    public static void main(String[] args) {

        String[] sentences = {

                «string  with    lot of   spaces»,

                «Hello, can I help you?»,

                «Java is a ‘programming’ language.»,

                «this is user-generated content»

                };

        for (String sentence : sentences){

            //get words from sentence

            String[] words = splitSentenceByWords(sentence);

            //print words

            System.out.println(Arrays.toString(words));            

        }

    }

    private static String[] splitSentenceByWords(String str){        

        //if string is empty or null, return empty array

        if(str == null || str.equals(«»))

            return new String[0];

        String[] words = str.split(» «);

        return words;        

    }

}

Output

[string, , with, , , , lot, of, , , spaces]

[Hello,, can, I, help, you?]

[Java, is, a, ‘programming’, language.]

[this, is, user-generated, content]

As you can see from the output, our code did not work as expected. The reason being is simple split by space is not enough to separate words from a string. Sentences may be separated by punctuation marks like dot, comma, question marks, etc.

In order to make the code handle all these punctuation and symbols, we will change our regular expression pattern from only space to all the punctuation marks and symbols as given below.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

package com.javacodeexamples.stringexamples;

import java.util.Arrays;

public class StringSplitByWords {

    public static void main(String[] args) {

        String[] sentences = {

                «string  with    lot of   spaces»,

                «Hello, can I help you?»,

                «Java is a ‘programming’ language.»,

                «this is [user-generated] content»

                };

        for (String sentence : sentences){

            //get words from sentence

            String[] words = splitSentenceByWords(sentence);

            //print words

            System.out.println(Arrays.toString(words));            

        }

    }

    private static String[] splitSentenceByWords(String str){        

        //if string is empty or null, return empty array

        if(str == null || str.equals(«»))

            return new String[0];

        String[] words = str.split(«[ !»\#$%&'()*+,-./:;<=>[email protected]\[\]^_`{|}~]+»);

        return words;        

    }

}

Output

[string, with, lot, of, spaces]

[Hello, can, I, help, you]

[Java, is, a, programming, language]

[this, is, user, generated, content]

This time we got the output as we wanted. The regex pattern [ !"\#$%&'()*+,-./:;<=>[email protected]\[\]^_`{|}~]+ includes almost all the punctuation and symbols that can be used in a sentence including space. We applied + at the end to match one or more instances of these to make sure that we do not get any empty words.

Instead of this pattern, you can also use \P{L} pattern to extract words from the sentence, where \P denotes POSIX expression and L denotes character class for word characters. You need to change the line with the split method as given below.

String[] words = str.split(«\P{L}+»);

Please note that \P{L} expression works for both ASCII and non-ASCII characters (i.e. accented characters like “café” or “kākā”).

This example is a part of the Java String tutorial with examples and the Java RegEx tutorial with examples.

Please let me know your views in the comments section below.

About the author

  • Author
  • Recent Posts

Rahim

I have a master’s degree in computer science and over 18 years of experience designing and developing Java applications. I have worked with many fortune 500 companies as an eCommerce Architect. Follow me on LinkedIn and Facebook.

Понравилась статья? Поделить с друзьями:
  • Java search for word
  • Javascript excel в браузере
  • Java regex word or word
  • Javascript excel to json
  • Java regex for any word