Reputation: 31
Hi I want to split a string as only two parts. i.e. I want to split this string only once.
EX: String-----> hai,Bye,Go,Run
I want to split the above string with comma(,) as two parts only
i.e
String1 ---> hai
String2 ---->Bye,Go,Run
Please help me how can I do it.
Upvotes: 3
Views: 1521
Reputation: 2911
String str="hai,Bye,Go,Run";
//String 1
String str1=str.substring(0,str.indexOf(','));
//String 1
String str1=str.substring(str.indexOf(',')+1,str.length);
done :)
Upvotes: 0
Reputation: 7790
Use String.split(String regex, int limit) method:
String[] result = string.split(",", 2);
Upvotes: 9
Reputation: 845
You can use the String method:
public String[] split(String regex, int limit)
e.g. (not tested)
String str = "hai,Bye,Go,Run"
str.split(",", 2);
Upvotes: 0
Reputation: 11327
String str = "hai,Bye,Go,Run";
String str1 = str.substring(0, str.indexOf(','));
String str2 = str.substring(str.indexOf(',')+1);
Upvotes: 0
Reputation: 19344
if you check out the Java Doc of string
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html
You'll find one of the methods is
split(String regex)
then what you want is to use a regex like "," to get a table of strings
Upvotes: 0
Reputation: 6387
This is a very basic Java knowledge... Have a look at String class definition before asking here: http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html
You should follow some Java tutorial before starting programming in Java.
Upvotes: 1