split string java by space code example

Example 1: space seperator in string in java

String[] splitted = "peter,james,thomas".split(",");

Example 2: hwo to split string by spaces in java

String lineOfCurrencies = "USD JPY AUD SGD HKD CAD CHF GBP EURO INR";
String[] currencies = lineOfCurrencies.split(" ");

Example 3: java split string without removing

String string = "Hello-World";
String[] split = string.Replace("-", "#-").Split("#"); 
//# is added to the Splitpoint so we can split on #

Output:
split = [Hello, -World]

Tags:

Java Example