1

How to create a String in Java by adding char by char. I have to do it like this, because i have to add a "," between al letters. I tried it like this, but it had not worked.

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
  • 1
    Use a StringBuilder. Commented Jun 3, 2017 at 15:52

5 Answers 5

2

You don't need to do it so complicated, and you should explicitly use StringBuilder for such string operations:

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

What about trailling comma?
@eg04lt3r It might be done in Java8 style if it's crucial. However, the author code does put the comma at the end of the string.
1

If you use empty strings instead of null and initialize it then it works.

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

Comments

0

I've put the errors in your code in comments.

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
}

You might want to try a StringBuilder. It's a lot more efficient than using string concatenation. Using the array a is also not really necessary. Have a look at this implementation.

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

Comments

0

Take a look at this:

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

Comments

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

Results

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.