想截取从开始到第三个逗号之间的内容
16 个解决方案
#1
用subString(0,16)
#2
就依本题而言:
str.substring(0,str.indexOf(",",12));
str.substring(0,str.indexOf(",",12));
#3
String str1=str.sunString(0,16);
#4
String[] arr = str.split(",");
String newStr = arr[0]+arr[1]+arr[2];
String newStr = arr[0]+arr[1]+arr[2];
#5
这样会少了逗号
#6
下面这样好点
String newStr = "";
for (int i = 0; i <3; i++) {
newStr += str.substring(0,str.indexOf(","));
str = str.substring(str.indexOf(",")+1);
}
System.out.println(newStr);
#7
补充逗号
String newStr = "";
for (int i = 0; i <3; i++) {
newStr += str.substring(0,str.indexOf(","))+",";
str = str.substring(str.indexOf(",")+1);
}
System.out.println(newStr.substring(0,newStr.lastIndexOf(",")));
#8
String[] arr = str.split(",");
String newStr = arr[0]+","+arr[1]+ ","+arr[2];
就行啦
String newStr = arr[0]+","+arr[1]+ ","+arr[2];
就行啦
#9
String ss = "111,222,333,444,555";
int i = 0;
int si = -1;
int begin = 0;
while (i++ < 3 && (si = ss.indexOf(',',begin))!=-1) {
begin = si + 1;
}
System.out.println(ss.substring(0, si));
#10
newStr = "";
nPos := 1;
for (int i = 0; i <3; i++) {
nPos = str.indexOf(",", nPos);
if( nPos<=0)
return;
}
newStr = str.subString( 0, nPos -1);
nPos := 1;
for (int i = 0; i <3; i++) {
nPos = str.indexOf(",", nPos);
if( nPos<=0)
return;
}
newStr = str.subString( 0, nPos -1);
#11
++
#12
+++
#13
int index = str.indexOf(str.indexOf(str,indexOf(",") + 1, ",") + 1, ",");
newStr = str.subString(0, index);
newStr = str.subString(0, index);
#14
非常感谢你的回答
#15
非常感谢,你的方法同楼上的一样可以
#16
感谢你一直关注本帖.但是从方法效率上操作.9楼和十楼略胜一筹
非常感谢.
#1
用subString(0,16)
#2
就依本题而言:
str.substring(0,str.indexOf(",",12));
str.substring(0,str.indexOf(",",12));
#3
String str1=str.sunString(0,16);
#4
String[] arr = str.split(",");
String newStr = arr[0]+arr[1]+arr[2];
String newStr = arr[0]+arr[1]+arr[2];
#5
这样会少了逗号
#6
下面这样好点
String newStr = "";
for (int i = 0; i <3; i++) {
newStr += str.substring(0,str.indexOf(","));
str = str.substring(str.indexOf(",")+1);
}
System.out.println(newStr);
#7
补充逗号
String newStr = "";
for (int i = 0; i <3; i++) {
newStr += str.substring(0,str.indexOf(","))+",";
str = str.substring(str.indexOf(",")+1);
}
System.out.println(newStr.substring(0,newStr.lastIndexOf(",")));
#8
String[] arr = str.split(",");
String newStr = arr[0]+","+arr[1]+ ","+arr[2];
就行啦
String newStr = arr[0]+","+arr[1]+ ","+arr[2];
就行啦
#9
String ss = "111,222,333,444,555";
int i = 0;
int si = -1;
int begin = 0;
while (i++ < 3 && (si = ss.indexOf(',',begin))!=-1) {
begin = si + 1;
}
System.out.println(ss.substring(0, si));
#10
newStr = "";
nPos := 1;
for (int i = 0; i <3; i++) {
nPos = str.indexOf(",", nPos);
if( nPos<=0)
return;
}
newStr = str.subString( 0, nPos -1);
nPos := 1;
for (int i = 0; i <3; i++) {
nPos = str.indexOf(",", nPos);
if( nPos<=0)
return;
}
newStr = str.subString( 0, nPos -1);
#11
++
#12
+++
#13
int index = str.indexOf(str.indexOf(str,indexOf(",") + 1, ",") + 1, ",");
newStr = str.subString(0, index);
newStr = str.subString(0, index);
#14
非常感谢你的回答
#15
非常感谢,你的方法同楼上的一样可以
#16
感谢你一直关注本帖.但是从方法效率上操作.9楼和十楼略胜一筹
非常感谢.