Java 中将字符串数组转换为字符串
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Test {
public static void main(String[] args) {
String[] str1 = {"Geeks", "ForGeeks", "GeeksForGeeks"};
String[] str2 = {"G", "e", "e", "k", "s"};
System.out.println(convertArrayToString(str1));
System.out.println(convertArrayToString(str2));
Set set = new HashSet<>(Arrays.asList(str1));
System.out.println(String.join(",", set));
}
public static String convertArrayToString(String[] strArr) {
if (strArr == null || strArr.length == 0) {
return "";
}
String res = "";
for (int i = 0, len = strArr.length; i < len; i++) {
res += strArr[i];
if (i < len - 1) {
res += ",";
}
}
return res;
}
}
Geeks,ForGeeks,GeeksForGeeks
G,e,e,k,s
ForGeeks,Geeks,GeeksForGeeks