44

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.

2
  • 1
    not duplicate as this question is for splitting with variable length white space Commented Jun 19, 2019 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 Commented Oct 22, 2019 at 21:22

7 Answers 7

92

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.

Sign up to request clarification or add additional context in comments.

Comments

26

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 

Comments

7

Use a regular expression.

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

Comments

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. 

1 Comment

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+");
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.

Comments

0
String spliter="\\s+"; String[] temp; temp=mystring.split(spliter); 

Comments

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()); } } 

3 Comments

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 can u pls illustrate why split is better than StringTokenizer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.