Android中String.join的替代方案?

[英]Alternative for String.join in Android?


I want to concatenate an ArrayList with commas as separators. I found this answer, stating it's possible to use String.join in Java.

我想將一個ArrayList與逗號連接為分隔符。我找到了這個答案,說明可以在Java中使用String.join。

When I try to use this however, Android Studio gives the following error:

但是,當我嘗試使用此功能時,Android Studio會出現以下錯誤:

Cannot resolve method 'join(java.lang.String, java.lang.String, java.lang.String, java.lang.String)'

無法解析方法'join(java.lang.String,java.lang.String,java.lang.String,java.lang.String)'

Is there a good, concise alternative for Android Studio (instead of using a for loop)?

Android Studio是否有一個好的,簡潔的替代方案(而不是使用for循環)?

3 个解决方案

#1


87  

You can use TextUtils.join instead:

您可以使用TextUtils.join代替:

String result = TextUtils.join(", ", list);

(String.join was added in Java 8, which is why you can't use it in Android.)

(在Java 8中添加了String.join,這就是為什么你不能在Android中使用它。)

#2


4  

You can use this

你可以用它

TextUtils.join(", ", your_list);

#3


4  

String[] List ={"<html>","<body>","<title>"};
String abc;       
abc =TextUtils.join("\n", List);
textmsg.getText().insert(textmsg.getSelectionStart(), abc);

result:

結果:

<html>
<body>
<title>

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/11/19/72980f12485d18f56def0dacda9fdbec.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com