繁体   English   中英

如何通过在Java中通过char添加char从char数组创建字符串

[英]how to create a string from char array by adding char by char in java

如何通过按字符添加char在Java中创建字符串。 我必须这样做,因为我必须在所有字母之间添加一个“,”。 我这样尝试过,但没有成功。

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;
}

如果您使用空字符串而不是null并对其进行初始化,那么它将起作用。

String t = "foobarbaz";
int l = t.length();
char[] a;
a = new char[l];
String rel = ",";
String ret = "";
for (int i = 0; i<l; i++){
    a[i] = t.charAt(i);
}
for (int v = 0; v<l; v++){
    ret += a[v];
    ret += rel;
}
System.out.println(ret);

您不需要做的那么复杂,您应该为此类字符串操作显式使用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);
}

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

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());

看看这个:

//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);
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,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM