Adi
Adi

Reputation: 1455

How to remove extra spaces from arraylist

I have to retrieve values from database and add it into arraylist .Here is my code ..

 ArrayList country = new ArrayList();
 strQuery = "Select * from country";

        rs = conexiondb.Consulta(strQuery);

        while (rs.next()) {
            String toc = rs.getString("country");
            country.add(toc);
        }

        out.print(country);
        System.out.println(country);
        out.close();

i have added all the values retrieved from database into country..and here is the values present in the country..

[APAC, North America, South America, Europe]

Now as per my need i have to remove space after each comma values and make it like this..

[APAC,North America,South America,Europe]

Please guys help me to solve this..

Thanks in advance.

Upvotes: 1

Views: 9281

Answers (5)

Acewin
Acewin

Reputation: 1727

If memory is not the issue. store it in a separate StringBuilder

This should not be a difficult problem. It is just a bit of manupluation.

In case you want comma separated value to have space storing in arraylist will give you that.

If you need the result to start and end with square braces with inside values being comma separated without any space in between the strings.

    ArrayList <String> country = new ArrayList<String>();

    ArrayList country = new ArrayList();
    strQuery = "Select * from country";

    rs = conexiondb.Consulta(strQuery);
    StringBuilder commaSeparated = new StringBuilder("[");

    while (rs.next()) {
        String toc = (rs.getString("country")).trim();
        commaSeparated.append(toc).append(",");
        country.add(toc);
    }

    commaSeparated.append("]");

    out.print(country);
    System.out.println(country);
    System.out.println(commaSeparated.toString());
    out.close();

Upvotes: 0

BeeOnRope
BeeOnRope

Reputation: 64925

The spaces aren't coming from the database at all, they are just a function of the way ArrayList.toString() (and in fact all of the JDK Collection classes which inherit from AbstractCollection) pretty prints your list.

You can tell this is the case either by looking at the documentation for this class, or by noting that there is no space before the initial [.

I recommend Guava Joiner for full control over your output.

Joiner.on(',').join(country);

will give the String you want.

Upvotes: 1

Juned Ahsan
Juned Ahsan

Reputation: 68715

ArrayList toString method prints the collection using a space after each comma. If you want to print the list without spaces, then you need to write a new method to do that.

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347214

What you are seeing is how ArrayList formats it's contents, not the results of your query, try something like...

for (String name : country) {
    System.out.println("[" + name + "]");
}

To check. If there are still spaces in the output, then you can use String#trim when you extract the values from the database and before you place them in the List

If you need to format the String, you need to provide you own mechanism, for example...

List<String> values = new ArrayList<>();
values.add("APAC");
values.add("North America");
values.add("South America");
values.add("Europe");

System.out.println(values);

StringBuilder sb = new StringBuilder(128);
for (String value : values) {
    if (sb.length() > 0) {
        sb.append(",");
    }
    sb.append(value);
}
sb.insert(0, "[");
sb.append("]");

System.out.println(sb);

Which outputs...

[APAC, North America, South America, Europe]
[APAC,North America,South America,Europe]

Upvotes: 1

user207421
user207421

Reputation: 310916

You are relying on the default formatting provided by ArrayList.toString(). Don't do that. Write your own formatting code.

Upvotes: 1

Related Questions