How can I break a line

Hi, I want to split the string as two parts. those. I want to break this line only once.

EX: String-----> hai,Bye,Go,Run 

I want to split this line with a semicolon (,) into only two parts

ie

String1 ---> hai 

String2 ---->Bye,Go,Run

Please help me how can I do this.

+3
source share
7 answers

Use the String.split method (String regex, int limit) :

String[] result = string.split(",", 2);
+8
source
String[] result = string.split("\\s*,\\s*" ,2);
+2
source
String str = "hai,Bye,Go,Run";
String str1 = str.substring(0, str.indexOf(','));
String str2 = str.substring(str.indexOf(',')+1);
0

You can use the String method:

public String[] split(String regex, int limit)

eg. (not verified)

String str = "hai,Bye,Go,Run"
str.split(",", 2);
0
source
String str="hai,Bye,Go,Run";

// line 1

String str1=str.substring(0,str.indexOf(','));

// line 1

String str1=str.substring(str.indexOf(',')+1,str.length);

done :)

0
source

Source: https://habr.com/ru/post/1786091/


All Articles