Preferred Idiom for Joining a Collection of Strings in Java

后端 未结 7 1600
悲哀的现实
悲哀的现实 2020-11-30 11:20

Given a Collection of Strings, how would you join them in plain Java, without using an external Library?

Given these variables:

Collection

        
相关标签:
7条回答
  • 2020-11-30 11:52

    Your proposition is a possible solution, another common one is:

    for (String item: data) {
        sb.append(item)
        sb.append(separator);
    }
    String joined = sb.substring(0, sb.length() - separator.length())
    
    0 讨论(0)
提交回复
热议问题