How to Split String in java?

How to Split String in java?

Below example shows how to split or brake a string. The split() method splits the string based on the given regular expression or delimiter, and returns the tokens in the form of array. Below example shows splitting string with space, and second split is based on any kind of spaces, that includes tab, enter, line breaks, etc.

 example:

public class Splittest {

public static void main(String a[]){

String s = "This program splits a string based on space";

String[] tokens = s.split(" ");

for(String s:tokens){

System.out.println(s);}

s = "This program splits a string based on space";

tokens = s.split("\\s+");}}

Read More →