0

I am getting the values from arraylist arr and storing those values in a string with comma separator. But for the last value also comma is adding. How can I remove that? Please help me...

My Code:

ArrayList<String> arr;  
String str;

    for (int i = 0; i < arr.size(); i++) {

                int kk = arr.size();
                System.out.println("splitting test" + arr.get(i));
                str += arr.get(i) + ",";

                System.out.println("result" + str);


            }

7 Answers 7

1

Do not add it at all:

ArrayList<String> arr;  
String str;

for (int i = 0; i < arr.size(); i++) {
   int kk = arr.size();
   System.out.println("splitting test" + arr.get(i));
   str += arr.get(i);
   if (i + 1 != arr.size()) {
     str+= ",";
   }
   System.out.println("result" + str);
}
Sign up to request clarification or add additional context in comments.

3 Comments

hi thank you..its working fine..Also another string data is like a,b,c,|,d,e,f,|,g,h,k,| Now I want to split the string first with "|" and then the result with ",". How can I do that? Please help me
Hi thank you for the link...It helped me...Also in my string a,b,c,|,d,e,f,|,g,h,k,|.....I want to remove comma after the | symbol. Please help me how to remove that.Thank you
you will cope with your problem if you simply first split on ",\\|," instead of splitting only on '|'. This should work.
1

What about this:

StringBuilder builder = new StringBuilder();
for(Object item : arrayOrIterable) builder.append(item).append(",");
String result = builder.deleteCharAt(builder.length() - 1).toString();

Comments

0

Try:

str.substring(0, str.length()-1);

Comments

0

If you try this, I think is should work.

ArrayList<String> arr;  
String str;

    for (int i = 0; i < arr.size(); i++) {

                int kk = arr.size();
                System.out.println("splitting test" + arr.get(i));
                str += arr.get(i) + i!=arr.size()-1?"":",";

                System.out.println("result" + str);


            }

Comments

0

You can always write like this.

String str;

        for (int i = 0; i < arr.size(); i++) {

            int kk = arr.size();
            System.out.println("splitting test" + arr.get(i));
            // It is your old code str += arr.get(i) + ",";
            // Insted write like this

            str += arr.get(i);
            if (i < arr.size() - 1) {
                str += ",";
            }
            System.out.println("result" + str);
        }

Comments

0

add one condition in for loop

if(i == arr.size()-1)
{
    str += arr.get(i);
}
else
{
   str += arr.get(i) + ",";

}

Comments

0

You have to pass array list to this function. it will return comma separated values.

public static <T> String getReasonArray(Collection<T> values) {
    if (values == null || values.isEmpty()) return "";
    StringBuilder result = new StringBuilder();
    for (T val : values) {
        result.append(val);
        result.append(",");
    }
    return result.substring(0, result.length() - 1);
}

1 Comment

Can you tell how / why this works and what OP was doing wrong?

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.