在Java中构建分隔项字符串的最佳方法是什么?

在Java中构建分隔项字符串的最佳方法是什么?

在Java应用程序中工作时,我最近需要组装一个以逗号分隔的值列表,以传递给另一个Web服务,而不知道预先会有多少元素。我能想到的最好的办法是:

public String appendWithDelimiter( String original, String addition, String delimiter ) {
    if ( original.equals( "" ) ) {
        return addition;
    } else {
        return original + delimiter + addition;
    }}String parameterString = "";if ( condition ) parameterString = appendWithDelimiter( parameterString, "elementName", "," );if ( anotherCondition ) parameterString = appendWithDelimiter( parameterString, "anotherElementName", "," );

我意识到这并不是特别有效,因为到处都在创建字符串,但我追求的是清晰而不是优化。

在Ruby中,我可以这样做,这感觉更优雅:

parameterArray = [];parameterArray << "elementName" if condition;parameterArray << "anotherElementName" if anotherCondition;parameterString = parameterArray.join(",");

但是因为Java缺少一个JOIN命令,所以我找不到任何类似的东西。

那么,用Java做这件事最好的方法是什么呢?


SMILET
浏览 498回答 3
3回答

红糖糍粑

预Java 8:Apache的Commonlang是您在这里的朋友-它提供了一个非常类似于您在Ruby中引用的连接方法:StringUtils.join(java.lang.Iterable,char)Java 8:Java 8通过StringJoiner和String.join()..下面的片段展示了如何使用它们:StringJoinerStringJoiner&nbsp;joiner&nbsp;=&nbsp;new&nbsp;StringJoiner(",");joiner.add("01").add("02").add("03");String&nbsp;joinedString&nbsp;=&nbsp;joiner.toString();&nbsp;//&nbsp;"01,02,03"String.join(CharSequence delimiter, CharSequence... elements))String&nbsp;joinedString&nbsp;=&nbsp;String.join("&nbsp;-&nbsp;",&nbsp;"04",&nbsp;"05",&nbsp;"06");&nbsp;//&nbsp;"04&nbsp;-&nbsp;05&nbsp;-&nbsp;06"String.join(CharSequence delimiter, Iterable<? extends CharSequence> elements)List<String>&nbsp;strings&nbsp;=&nbsp;new&nbsp;LinkedList<>();strings.add("Java");strings.add("is");strings.add("cool");String&nbsp;message&nbsp;=&nbsp;String.join("&nbsp;",&nbsp;strings);//message&nbsp;returned&nbsp;is:&nbsp;"Java&nbsp;is&nbsp;cool"

慕仙森

您可以编写一个在java.util.List上工作的小连接样式实用程序方法。public&nbsp;static&nbsp;String&nbsp;join(List<String>&nbsp;list,&nbsp;String&nbsp;delim)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;StringBuilder&nbsp;sb&nbsp;=&nbsp;new&nbsp;StringBuilder(); &nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;loopDelim&nbsp;=&nbsp;""; &nbsp;&nbsp;&nbsp;&nbsp;for(String&nbsp;s&nbsp;:&nbsp;list)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sb.append(loopDelim); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sb.append(s);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loopDelim&nbsp;=&nbsp;delim; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;sb.toString();}然后像这样使用它:&nbsp;&nbsp;&nbsp;&nbsp;List<String>&nbsp;list&nbsp;=&nbsp;new&nbsp;ArrayList<String>(); &nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;condition&nbsp;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list.add("elementName"); &nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;anotherCondition&nbsp;)&nbsp;list.add("anotherElementName"); &nbsp;&nbsp;&nbsp;&nbsp;join(list,&nbsp;",");

慕斯709654

这个谷歌番石榴图书馆有com.google.Common.base.Joiner类,帮助解决此类任务。样本:"My&nbsp;pets&nbsp;are:&nbsp;"&nbsp;+&nbsp;Joiner.on(",&nbsp;").join(Arrays.asList("rabbit",&nbsp;"parrot",&nbsp;"dog"));&nbsp;//&nbsp;returns&nbsp;"My&nbsp;pets&nbsp;are:&nbsp;rabbit,&nbsp;parrot,&nbsp;dog"Joine r.on("&nbsp;AND&nbsp;").join(Arrays.asList("field1=1"&nbsp;,&nbsp;"field2=2",&nbsp;"field3=3"));//&nbsp;returns&nbsp;"field1=1&nbsp;AND&nbsp;field2=2&nbsp;AND&nbsp;field3=3"Joiner.on(",").ski pNulls().join(Arrays.asList("London",&nbsp;"Moscow",&nbsp;null,&nbsp;"New&nbsp;York",&nbsp;null,&nbsp;"Paris"));//&nbsp;returns&nbsp;"London,Moscow,New&nbsp;York,Paris"Joiner.on(",&nbsp;"). useForNull("Team&nbsp;held&nbsp;a&nbsp;draw").join(Arrays.asList("FC&nbsp;Barcelona",&nbsp;"FC&nbsp;Bayern",&nbsp;null,&nbsp;null,&nbsp;"Chelsea&nbsp;FC",&nbsp;"AC&nbsp;Milan"));//&nbsp;returns&nbsp;"FC&nbsp;Barcel ona,&nbsp;FC&nbsp;Bayern,&nbsp;Team&nbsp;held&nbsp;a&nbsp;draw,&nbsp;Team&nbsp;held&nbsp;a&nbsp;draw,&nbsp;Chelsea&nbsp;FC,&nbsp;AC&nbsp;Milan"这是一个关于Guava字符串实用程序的文章.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java