How to split a String by space
What you have should work. If, however, the spaces provided are defaulting to... something else? You can use the whitespace regex:
str = "Hello I'm your String";
String[] splited = str.split("\\s+");
This will cause any number of consecutive spaces to split your string into tokens.
split string by arbitrary number of white spaces
Just use my_str.split()
without ' '
.
More, you can also indicate how many splits to perform by specifying the second parameter:
>>> ' 1 2 3 4 '.split(None, 2)
['1', '2', '3 4 ']
>>> ' 1 2 3 4 '.split(None, 1)
['1', '2 3 4 ']
How to split a string with any whitespace chars as delimiters
Something in the lines of
myString.split("\\s+");
This groups all white spaces as a delimiter.
So if I have the string:
"Hello[space character][tab character]World"
This should yield the strings "Hello"
and "World"
and omit the empty space between the [space]
and the [tab]
.
As VonC pointed out, the backslash should be escaped, because Java would first try to escape the string to a special character, and send that to be parsed. What you want, is the literal "\s"
, which means, you need to pass "\\s"
. It can get a bit confusing.
The \\s
is equivalent to [ \\t\\n\\x0B\\f\\r]
.
Splitting string on multiple spaces in java
str.split("\\s+")
would work. The +
at the end of the regular-expression, would treat multiple spaces the same as a single space. It returns an array of strings (String[]
) without any " "
results.
Split a string with unknown number of spaces as separator in Python
If you don't pass any arguments to str.split()
, it will treat runs of whitespace as a single separator:
>>> ' 1234 Q-24 2010-11-29 563 abc a6G47er15'.split()
['1234', 'Q-24', '2010-11-29', '563', 'abc', 'a6G47er15']
Split string with variable number of spaces
You can use StringSplitOptions.RemoveEmptyEntries
enumeration with String.Split
method like;
The return value does not include array elements that contain an empty
string
string s = "7 300685 1235 200017 200018 200019";
var array = s.Split(new []{" "}, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in array)
{
Console.WriteLine(item);
}
Output will be;
7
300685
1235
200017
200018
200019
Here a demonstration
.
Splitting a string with multiple spaces
Since the argument to split()
is a regular expression, you can look for one or more spaces (" +"
) instead of just one space (" "
).
String[] array = s.split(" +");
Split a string by any number of spaces
Just use strsplit
with \\s+
to split on:
x <- "10012 ---- ---- ---- ---- CAB UNCH CAB"
x
# [1] "10012 ---- ---- ---- ---- CAB UNCH CAB"
strsplit(x, "\\s+")[[1]]
# [1] "10012" "----" "----" "----" "----" "CAB" "UNCH" "CAB"
length(.Last.value)
# [1] 8
Or, in this case, scan
also works:
scan(text = x, what = "")
# Read 8 items
# [1] "10012" "----" "----" "----" "----" "CAB" "UNCH" "CAB"
Split string using variable spaces
You can use regular expressions with the split
function to match any number of spaces.
var lines = line.split(/\s+/);
The \s+
regular expression matches one or more spaces.
A runnable example with the strings you provided:
var lines = [
"0.0 0.2 88 /usr/sbin/securityd",
"47.0 0.3 7770 node",
"1.0 2.5 585 /Applications/PhpStorm.app/Contents/MacOS/phpstorm"
];
for (var i in lines)
console.log(lines[i].split(/\s+/));
JavaScript split String with white space
You could split the string on the whitespace and then re-add it, since you know its in between every one of the entries.
var string = "text to split";
string = string.split(" ");
var stringArray = new Array();
for(var i =0; i < string.length; i++){
stringArray.push(string[i]);
if(i != string.length-1){
stringArray.push(" ");
}
}
Update: Removed trailing space.
Related Topics
The Condition Has Length > 1 and Only the First Element Will Be Used in If Else Statement
Should I Use a Data.Frame or a Matrix
How to Change the Color Value of Just One Value in Ggplot2's Scale_Fill_Brewer
How to Call a Function Using the Character String of the Function Name in R
Export a Graph to .Eps File with R
How to Change the Background Color of a Plot Made with Ggplot2
Set Certain Values to Na with Dplyr
Plot Data in Descending Order as Appears in Data Frame
Solution. How to Install_Github When There Is a Proxy
Dplyr: Lead() and Lag() Wrong When Used with Group_By()
Extract Names of Objects from List
How to Override a Non-Visible Function in the Package Namespace