简体   繁体   English

如何安全地删除ArrayList上的逗号?

[英]How to safely remove the commas on an ArrayList?

I have an Arraylist where I place float values. 我有一个Arraylist放置浮点值。

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. 问题是Arraylist将逗号放在单独的值上。 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 我不确定您可以在不破坏数据的情况下从到字符串擦除',',我建议使用for()获取信息

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

Like this you will get all the information inside a String without the ']','[' or ','. 这样,您将在String中获得所有信息,但不包含']','['或','。 Hope it helps you. 希望对您有帮助。

You might want to look into using a StringBuilder in this case, for example: 在这种情况下,您可能想研究使用StringBuilder,例如:

StringBuilder builder = new StringBuilder();

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

String formatedString = builder.toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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