How to Split a String by Space

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.

How to split() a String while maintaining whitespace

You could use lookahead/lookbehind assertions:

String[] words = "hello  world".split("((?<=\\s+)|(?=\\s+))");

where (?<=\\s+) and (?=\\s+) are zero-width groups.

splitting string by spaces and new lines

I hope i understood your question right but if you're looking to extract the number and return them in the specific format you could go like this :

    // Assuming the String would be like a repetition of [word][space][number][b][\n]
String testString = "xyz 100b\nabc 200b\ndef 400b";

// Split by both endOfLine and space
String[] pieces = testString.split("\n| ");

String answer = "";

// pair index should contain the word, impair is the integer and 'b' letter
for (int i = 0; i < pieces.length; i++) {
if(i % 2 != 0 ) {
answer = answer + "answer " + ((i/2)+1) + ": " + pieces[i] + "\n";
}
}
System.out.println(answer);

This is the value of answer after execution :

answer 1: 100b
answer 2: 200b
answer 3: 400b

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.

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(" +");

How to split a string from the first space occurrence only Java

Must be some around this:

String nickname = temp.substring(0, temp.indexOf(' '));
String content = temp.substring(temp.indexOf(' ') + 1);

spliting a string by space and dot and comma at the same time

The method split can be used with a Regex pattern, so you can match more elaborated cases to split your string.

A matching pattern for your case would be:

[ \.,]+

Regex Exaplanation:

[ .,]+ - The brackets create Character Set, that will match any character in the set.

[ .,]+ - The plus sign is a Quantifier, it will match the previous token (the character set) one or more times, this solves the problem where the tokens are following one another, creating empty strings in the array.

You can test it with the following code:

class Main {
public static void main(String[] args) {
String str = "Hello, World!, StackOverflow. Test Regex";
String[] split = str.split("[ .,]+");
for(String s : split){
System.out.println(s);
}
}
}

The output is:

Hello

World!

StackOverflow

Test

Regex

Java split String that start with space

You can call string.trim()and then string.split(" "). The trim() method removes spaces before the first non-space-character and after the last non-space-character.

How to split a string by space and special character in javascript?

You just need to fix up your regular expression. Instead, use:

/\W+/

Here we are splitting by non-word characters (\W+) (consecutive)