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?
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.
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.
The split() method divides the string at the specified regex and returns an array of substrings.
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.
Use split()
method
Eg:
String s = "I want to walk my dog"; String[] arr = s.split(" "); for ( String ss : arr) { System.out.println(ss); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With