I'm trying to find a way to split a String into an array of String(s), and I need to split it whenever a white spice is encountered, example
"hi i'm paul"
into"
"hi" "i'm" "paul"
How do you represent white spaces in split() method using RegularExpression?
split() The method split() splits a String into multiple Strings given the delimiter that separates them. The returned object is an array which contains the split Strings. We can also pass a limit to the number of elements in the returned array.
The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.
In PowerShell, we can use the split operator (-Split) to split a string text into array of strings or substrings. The split operator uses whitespace as the default delimiter, but you can specify other characters, strings, patterns as the delimiter. We can also use a regular expression (regex) in the delimiter.
As the name suggests, a Java String Split() method is used to decompose or split the invoking Java String into parts and return the Array. Each part or item of an Array is delimited by the delimiters(“”, “ ”, \\) or regular expression that we have passed. The return type of Split is an Array of type Strings.
You need a regular expression like "\\s+"
, which means: split whenever at least one whitespace is encountered. The full Java code is:
try { String[] splitArray = input.split("\\s+"); } catch (PatternSyntaxException ex) { // }
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