35

Possible Duplicate:
How to split a String by space

I need help while parsing a text file. The text file contains data like

This is     different type   of file.
Can not split  it    using ' '(white space)

My problem is spaces between words are not similar. Sometimes there is single space and sometimes multiple spaces are given.

I need to split the string in such a way that I will get only words, not spaces.

Community
  • 1
  • 1
Sachin Mhetre
  • 4,225
  • 10
  • 39
  • 65
  • 1
    not duplicate as this question is for splitting with variable length white space – JGilmartin Jun 19 '19 at 16:45
  • I don't see how this is a duplicate at all. The "possible duplicate" does not address the corner case of multiple-spaces, which is the main point of the question. For reference, this question is also the first result on google when you search for `java string split multiple spaces` – RvPr Oct 22 '19 at 21:22

7 Answers7

74

str.split("\\s+") would work. The + at the end of the regular-expression, would treat multiple spaces the same as a single space. It returns an array of strings (String[]) without any " " results.

RvPr
  • 806
  • 1
  • 7
  • 19
Bhesh Gurung
  • 48,464
  • 20
  • 87
  • 139
22

You can use Quantifiers to specify the number of spaces you want to split on: -

    `+` - Represents 1 or more
    `*` - Represents 0 or more
    `?` - Represents 0 or 1
`{n,m}` - Represents n to m

So, \\s+ will split your string on one or more spaces

String[] words = yourString.split("\\s+");

Also, if you want to specify some specific numbers you can give your range between {}:

yourString.split("\\s{3,6}"); // Split String on 3 to 6 spaces
Rohit Jain
  • 195,192
  • 43
  • 369
  • 489
6

Use a regular expression.

String[] words = str.split("\\s+");
alex
  • 438,662
  • 188
  • 837
  • 957
5

you can use regex pattern

public static void main(String[] args)
{
    String s="This is     different type   of file.";
    String s1[]=s.split("[ ]+");
    for(int i=0;i<s1.length;i++)
    {
        System.out.println(s1[i]);
    }
}

output

This
is
different
type
of
file.
RobEarl
  • 7,556
  • 6
  • 31
  • 48
Bhavik Shah
  • 4,765
  • 2
  • 21
  • 39
  • Your solution only splits by blanks, not by any other whitespace characters such as `\t\n\x0B\f\r`. Use the character class `\s` (any whitespace character) instead, as described by the others. `String[] words = yourString.split("\\s+"); ` – jlordo Oct 26 '12 at 06:12
0

you can use
replaceAll(String regex, String replacement) method of String class to replace the multiple spaces with space and then you can use split method.

Techie Manoj
  • 432
  • 3
  • 14
0
String spliter="\\s+";
String[] temp;
temp=mystring.split(spliter);
Vivek Maskara
  • 1,035
  • 10
  • 25
0

I am giving you another method to tockenize your string if you dont want to use the split method.Here is the method

public static void main(String args[]) throws Exception
{
    String str="This is     different type   of file.Can not split  it    using ' '(white space)";
    StringTokenizer st = new StringTokenizer(str, " "); 
    while(st.hasMoreElements())
    System.out.println(st.nextToken());
}
 }
saurabh j
  • 89
  • 1
  • 1
  • 7
  • And why wouldn't he want to use the split method, given that it is a better way to go than `StringTokenizer`? Please stop using `StringTokenizer`. – Rohit Jain Oct 26 '12 at 06:24
  • Rohit can u pls illustrate why split is better than StringTokenizer – saurabh j Oct 26 '12 at 07:02
  • 1
    You can see http://stackoverflow.com/questions/691184/scanner-vs-stringtokenizer-vs-string-split http://www.javamex.com/tutorials/regular_expressions/splitting_tokenisation_performance.shtml and http://stackoverflow.com/questions/5965767/performance-of-stringtokenizer-class-vs-split-method-in-java – Rohit Jain Oct 26 '12 at 07:16