2017-06-03 46 views
1

如何通過char添加char來在Java中創建字符串。 我必須這樣做,因爲我必須在字母之間加上「,」。 我試過這樣,但它沒有奏效。如何通過在char中添加char字符來創建char數組中的字符串

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

使用'StringBuilder'。 –

回答

1

如果您使用空字符串而不是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); 
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()); 
+0

Thx,非常有幫助... – xcynthos

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); 
} 
+0

拖放逗號怎麼樣? – eg04lt3r

+0

@ eg04lt3r如果它很重要,那麼可以用Java8風格完成。但是,作者代碼確實將逗號放在字符串的末尾。 –

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); 
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, 
相關問題