JAVA - Parsing into 2 strings total
I have a 开发者_开发技巧string which I want to split using a certain set of delimiters, but I would like to split it into 2 strings exactly. This means that if the string is "ax,yt,zr" I want to split it into "ax" and "yt,zr". What is the cleanest way to do this?
String.split(String regex, int limit)
for eaxmple:
"ax,yt,zr".split(",", 2);
You can use substring.
String text = "ax,yt,zr";
String strOne = text.substring(0, text.indexOf(','));
String strTwo = text.substring(text.indexOf(','));
String g = "ax,yt,zr";
String r = g.substring(0,g.indexOf(","));
String f = g.substring(g.indexOf(","),g.length());
System.out.println(r + " " + f);
indexOf: returns the index within this string of the first occurrence of the specified substring.
精彩评论