如何通过在java中使用char添加char来从char数组创建字符串

时间:2017-06-03 15:45:12

标签: java arrays string char

如何通过char添加char来在Java中创建String。 我必须这样做,因为我必须在al字母之间添加“,”。 我试过这样的,但它没有用。

String t;
int l = t.length();
char[] a;
a = new char[l];
String rel = ",";
String ret = null;

for (int i = 0; i<l; i++){
    a[i] = new Character(t.charAt(0));
}

for (int v = 0; v<l; v--){
    ret += a[v];
    ret += rel;
}

5 个答案:

答案 0 :(得分:1)

如果你使用空字符串而不是null并初始化它然后它工作。

console.log(this)

答案 1 :(得分:1)

你不需要这么复杂,你应该明确地使用StringBuilder进行这样的字符串操作:

String s = "abcdefg";

StringBuilder builder = new StringBuilder();

for (char c : s.toCharArray()) {
    builder.append(c).append(",");
}

// Alternatively, you can do it in this way
for (String symbol : s.split("")) {
    builder.append(symbol).append(",");
}

System.out.println(builder.toString());

// Java 8 (the result string doesn't have a comma at the end)
String collect = Arrays.stream(s.split("")).collect(Collectors.joining(","));

// Java8 StringJoiner
StringJoiner sj = new StringJoiner(",");
// StringJoiner sj = new StringJoiner(",", "", ",");
for (String str : s.split("")) {
    sj.add(str);
}

答案 2 :(得分:0)

我已将错误放在您的代码中。

String t;
int l = t.length();
char[] a;
a = new char[l];
String rel = ",";
String ret = null; //you initialize ret to null, it should be "";

for (int i = 0; i<l; i++){
    //you always set it to the character at position 0, you should do t.charAt(i)
    //you don't need to use the wrapper class just t.charAt(i) will be fine.
    a[i] = new Character(t.charAt(0)); 
}

for (int v = 0; v<l; v--){//you decrement v instead of incrementing it, this will lead to exceptions
    ret += a[v];
    ret += rel;//you always add the delimiter, note that this will lead to a trailing delimiter at the end
}

您可能想尝试使用StringBuilder。它比使用字符串连接更有效。使用数组a也不是必需的。看看这个实现。

String t = "Test";
StringBuilder builder = new StringBuilder();
if(t.length() > 0){
    builder.append(t.charAt(0));
    for(int i=1;i<t.length();i++){
        builder.append(",");
        builder.append(t.charAt(i));
    }
}
System.out.println(builder.toString());

答案 3 :(得分:0)

看看这个:

//Word to be delimited by commas    
    String t = "ThisIsATest";

    //get length of word. 
    int l = t.length(); //4

    char[] a;
    a = new char[l];
    // we will set this to a comma below inside the loop
    String rel = "";
    //set ret to empty string instead of null otherwise the word "null" gets put at the front of your return string
    String ret = "";

    for (int i = 0; i<l; i++){
        //you had 0 instead of 'i' as the parameter of t.charAt. You need to iterate through the elements of the string as well
        a[i] = new Character(t.charAt(i));
    }

    for (int v = 0; v<l; v++){
    /*set rel to empty string so that you can add it BEFORE the first element of the array and then afterwards change it to a comma
     this prevents you from having an extra comma at the end of your list.         */

        ret += rel;
        ret += a[v];
        rel = ",";
    } 
    System.out.println(ret);

答案 4 :(得分:0)

String text = "mydata";
char[] arrayText = text.toCharArray();
char[] arrayNew = new char[arrayText.length*2];
for(int i = 0, j = 0; i < arrayText.length; i++, j+=2){
    arrayNew[j] = arrayText[i];
    arrayNew[j+1] = ',';
}
String stringArray = new String(arrayNew);
System.out.println(stringArray);

结果

m,y,d,a,t,a,