简体   繁体   中英

How to safely remove the commas on an ArrayList?

I have an Arraylist where I place float values.

xEvent.add(fileTimeStamp);
            xEvent.add(x);
            xEvent.add(zCSV + "\n");


    String formatedString = xEvent.toString()
                                .replace("[", "") // remove the right bracket
                                .replace("]", "") // remove the left bracket

The problem is that the Arraylist places a comma to separate values. How can I get rid of this comma without getting rid of the comma that in some locales is used as the decimal mark? On the US we use the period as the decimal mark, so removing all commas would work, however on countries where they use the comma as the decimal mark I would end up ruining the data.

I'm not sure you can errase the ',' from the to string without ruining the data, I would recomend using a for() to get the information

String formatedString="";
for(String s: xEvent){
    formatedString += s;
}

Like this you will get all the information inside a String without the ']','[' or ','. Hope it helps you.

You might want to look into using a StringBuilder in this case, for example:

StringBuilder builder = new StringBuilder();

builder
  .append(fileTimeStamp)
  .append(x)
  .append(zCSV);

String formatedString = builder.toString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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