Java Android 中 String.join 的替代方案?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33802971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:03:07  来源:igfitidea点击:

Alternative for String.join in Android?

javaandroid

提问by Algorithm_NL

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

我想用逗号作为分隔符连接一个 ArrayList。我找到了这个答案,说明可以String.join在 Java 中使用。

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 循环)?

采纳答案by Jon Skeet

You can use TextUtils.joininstead:

您可以TextUtils.join改用:

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

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

String.join是在 Java 8 中添加的,这就是你不能在 Android 中使用它的原因。)

回答by Zeeshan Ahmed

You can use this

你可以用这个

TextUtils.join(", ", your_list);

回答by BREI

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

result:

结果:

<html>
<body>
<title>
<html>
<body>
<title>