java基础,取出字符串前后逗号
2018-10-31 本文已影响15人
墨色尘埃
根据","截取字符串长度,for循环
@Test
public void testTiii() {
String a = "";
String b = "";
String c = "hhh";
//第一
for (int i = 0; i < 2; i++) {
a += b + ",";
}
//第二
for (int i = 0; i < 2; i++) {
a += b + ",";
b = "ddsd";
}
System.out.println("a=" + a);
//第三
for (int i = 0; i < 2; i++) {
a += c + ",";
}
System.out.println("a=" + a);
for (int i = 0; i < a.split(",").length; i++) {
if (a.startsWith(",")) {
a = a.substring(1,a.length());
}
if (a.endsWith(",")) {
a = a.substring(0, a.length() - 1);
}
}
System.out.println("a=" + a);
}