Split string into individual words Java

Java

Java Problem Overview


I would like to know how to split up a large string into a series of smaller strings or words. For example:

> I want to walk my dog.

I want to have a string: "I", another string:"want", etc.

How would I do this?

Java Solutions


Solution 1 - Java

Use split() method

Eg:

String s = "I want to walk my dog";
String[] arr = s.split(" ");    

for ( String ss : arr) {
    System.out.println(ss);
}

Solution 2 - Java

As a more general solution (but ASCII only!), to include any other separators between words (like commas and semicolons), I suggest:

String s = "I want to walk my dog, cat, and tarantula; maybe even my tortoise.";
String[] words = s.split("\\W+");

The regex means that the delimiters will be anything that is not a word [\W], in groups of at least one [+]. Because [+] is greedy, it will take for instance ';' and ' ' together as one delimiter.

Solution 3 - Java

A regex can also be used to split words.

\w can be used to match word characters ([A-Za-z0-9_]), so that punctuation is removed from the results:

String s = "I want to walk my dog, and why not?";
Pattern pattern = Pattern.compile("\\w+");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
	System.out.println(matcher.group());
}

Outputs:

I
want
to
walk
my
dog
and
why
not

See Java API documentation for Pattern

Solution 4 - Java

See my other answer if your phrase contains accentuated characters :

String[] listeMots = phrase.split("\\P{L}+");

Solution 5 - Java

Yet another method, using StringTokenizer :

String s = "I want to walk my dog";
StringTokenizer tokenizer = new StringTokenizer(s);

while(tokenizer.hasMoreTokens()) {
    System.out.println(tokenizer.nextToken());
}

Solution 6 - Java

To include any separators between words (like everything except all lower case and upper case letters), we can do:

String mystring = "hi, there,hi Leo";
String[] arr = mystring.split("[^a-zA-Z]+");
for(int i = 0; i < arr.length; i += 1)
{
     System.out.println(arr[i]);
}

Here the regex means that the separators will be anything that is not a upper or lower case letter [^a-zA-Z], in groups of at least one [+].

Solution 7 - Java

You can use split(" ") method of the String class and can get each word as code given below:

String s = "I want to walk my dog";
String []strArray=s.split(" ");
for(int i=0; i<strArray.length;i++) {
     System.out.println(strArray[i]);
}

Solution 8 - Java

Use split()

String words[] = stringInstance.split(" ");

Solution 9 - Java

you can use Apache commons' StringUtils class

String[] partsOfString = StringUtils.split("I want to walk my dog", StringUtils.SPACE)

Solution 10 - Java

This regex will split word by space like space, tab, line break:

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

Solution 11 - Java

StringTokenizer separate = new StringTokenizer(s, " ");
String word = separate.nextToken();
System.out.println(word);

Solution 12 - Java

Java String split() method example

 public class SplitExample{  
    	public static void main(String args[]){  
    		String str="java string split method";  
    		String[] words=str.split("\\s");//splits the string based on whitespace  
     
    		for(String word:words){  
    			System.out.println(word);  
    		}  
    	}
    }

Solution 13 - Java

class test{
           
    public static void main(String[] args){
            	StringTokenizer st= new StringTokenizer("I want to walk my dog.");
                
                while (st.hasMoreTokens())
                    System.out.println(st.nextToken());
         
            }
        }

Solution 14 - Java

String[] str = s.split("[^a-zA-Z]+");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfoshoView Question on Stackoverflow
Solution 1 - JavaKumar Vivek MitraView Answer on Stackoverflow
Solution 2 - JavaAnton TeodorView Answer on Stackoverflow
Solution 3 - JavaPeteView Answer on Stackoverflow
Solution 4 - JavaPierre CView Answer on Stackoverflow
Solution 5 - JavaKaoView Answer on Stackoverflow
Solution 6 - JavaAkib ImtiazView Answer on Stackoverflow
Solution 7 - JavaAKTView Answer on Stackoverflow
Solution 8 - JavajmjView Answer on Stackoverflow
Solution 9 - JavaGagan ChouhanView Answer on Stackoverflow
Solution 10 - JavaKyo HuuView Answer on Stackoverflow
Solution 11 - Javach NomanView Answer on Stackoverflow
Solution 12 - JavaNeeraj GahlawatView Answer on Stackoverflow
Solution 13 - JavaRakesh palView Answer on Stackoverflow
Solution 14 - JavaYan NanView Answer on Stackoverflow