Join strings with different last delimiter

前端 未结 7 1139
感情败类
感情败类 2021-01-04 12:04

Using stream.collect(Collectors.joining(\", \")) I can easily join all the strings of my stream delimited by a comma. A possible result would be \"a, b, c

7条回答
  •  鱼传尺愫
    2021-01-04 12:48

    String str = "a , b , c , d";
    String what_you_want = str.substring(0, str.lastIndexOf(","))
            + str.substring(str.lastIndexOf(",")).replace(",", "and");
    
    // what_you_want is : a , b , c and d
    

提交回复
热议问题