我想将字符串与“,”一起从2到6。当我执行下面的代码时,我得到了输出,但是它以逗号开头。 String name=""; String s = "1,two,three,four,five,six,seven"; //this is a sample string, original string might contain more words separated by "," String[] split = s.split(","); System.out.println("Splitted Length: " +split.length); if(split.length>2) { for(int i=1; i<split.length-1;i++) { name = name+","+split[i]; } } System.out.println(name);输出: Splitted Length: 7 ,two,three,four,five,six如何避免第一个逗号
3 回答

DIEA
TA贡献1820条经验 获得超3个赞
请停止使用那些for循环。不要发明新事物。知道API。
List<String> valuesList = Arrays.asList(array).subList(x, y); String newValuesString = String.join(",", valuesList);

FFIVE
TA贡献1797条经验 获得超6个赞
您可以使用Java8Stream和Pattern为。
String result = Pattern.compile(",").splitAsStream(s)
.skip(1)
.collect(Collectors.joining(","));
添加回答
举报
0/150
提交
取消