Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string into individual words Java

Tags:

java

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?

like image 603
fosho Avatar asked Jul 30 '12 16:07

fosho


People also ask

How do you separate words in Java?

Usually, words are separated by just one white space between them. In order to split it and get the array of words, just call the split() method on input String, passing a space as regular expression i.e." ", this will match a single white space and split the string accordingly.

How do I separate words from a string?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

What is split () in Java?

The split() method divides the string at the specified regex and returns an array of substrings.

Is there a way to split string in Java?

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.


1 Answers

Use split() method

Eg:

String s = "I want to walk my dog"; String[] arr = s.split(" ");      for ( String ss : arr) {     System.out.println(ss); } 
like image 70
Kumar Vivek Mitra Avatar answered Oct 03 '22 12:10

Kumar Vivek Mitra