Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split String into an array of String

Tags:

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?

like image 596
JBoy Avatar asked May 22 '11 06:05

JBoy


People also ask

How do I split a string into multiple strings?

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.

How do you split a string into an array in Python?

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.

How do I split a string into an array in PowerShell?

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.

Can you split a string array in Java?

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.


1 Answers

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) {     //  } 
like image 200
Staffan Nöteberg Avatar answered Sep 17 '22 23:09

Staffan Nöteberg