Bootstrap

java截取字符串

Java字符串截取方法
String str = "abcdefgh_123"
1.根据下标截取:
str = str.substring(0,3);

结果为:abc
2.根据字符截取:
//截取_之前字符串:
str = str.substring(0, str.indexOf("_"));

结果为:abcdefgh

------------------------------------------------------------------

//截取_之后字符串:
String str1 = str.substring(0, str.indexOf("_"));
String str2 = str.substring(str1.length()+1, str.length());

str2结果为:123
;