将字符串中的空格替换为不在JAVA中工作的replaceAll

将字符串中的空格替换为不在JAVA中工作的replaceAll,java,Java,我尝试将空格移到包含int类型值的字符串中 我用scanner方法读取.csv文件。 我使用类来设置/获取数据。 我将数据格式化为类的setter。 输入数据示例: String Pu_ht = "1 635,90"; 基本示例: /** * @param Pu_ht the Pu_ht to set */ public void setPu_ht(String Pu_ht) { this.Pu_ht = Pu_ht.replace(",", ".").replace(".00",

我尝试将空格移到包含int类型值的字符串中

我用scanner方法读取.csv文件。 我使用类来设置/获取数据。 我将数据格式化为类的setter。 输入数据示例:

String Pu_ht = "1 635,90";
基本示例:

/**
 * @param Pu_ht the Pu_ht to set
 */
public void setPu_ht(String Pu_ht) {
    this.Pu_ht = Pu_ht.replace(",", ".").replace(".00", "");
}
示例:

  /**
 * @param Pu_ht the Pu_ht to set
 */
public void setPu_ht(String Pu_ht) {
    this.Pu_ht = Pu_ht.replace(",", ".").replace(".00", "").replaceAll("\\s+", "");
}
其他例子:

 /**
 * @param Pu_ht the Pu_ht to set
 */
public void setPu_ht(String Pu_ht) {
    this.Pu_ht = Pu_ht.replace(",", ".").replace(".00", "").replaceAll(" ", "");
}
输出数据示例:1635.90

我做了很多尝试,但都不管用

致意

编辑:

我的代码:

 public void requete_pommes() throws IOException, ClassNotFoundException, SQLException {
    // open file input stream
    BufferedReader reader = new BufferedReader(new FileReader(filename));

    // read file line by line
    String line = null;
    Scanner scanner = null;
    int index = 0;
    List<Pommes> pomList = new ArrayList<>();
    boolean firstLine = false;

    while ((line = reader.readLine()) != null) {
        if (!(line.equals(";;;;TOTAL HT"))) {

            if (!(line.equals(";;;;"))) {

                Pommes pom = new Pommes();
                scanner = new Scanner(line);
                scanner.useDelimiter(";");
                while (scanner.hasNext()) {
                    String data = scanner.next();


                    pom.setNumero_compte("21826");
                    if ((index == 0)) {
                        pom.setReference(data);
                    } else if ((index == 1)) {
                        pom.setDesignation(data);
                    } else if ((index == 2)) {
                        pom.setQte(data);
                    } else if ((index == 3)) {
                         if(data.equals("1 635,90")){
                              data = data.replaceAll("\\s","");
                              System.err.println("data: " + data);
                         }
                        pom.setPu_ht(data);
                    } else if ((index == 4)) {
                        pom.setMontant_HT(data);
                    } else {
                        System.out.println("invalid data::" + data);
                    }
                    pom.setNumero_commande("1554");

                    index++;
                }
                index = 0;
                pomList.add(pom);

                requeteCorps = "(( SELECT codea FROM article WHERE tarif7 != 'O' AND tarif8 = 'O' AND pvente > 0 AND  COALESCE(trim( reffou), '') != '' AND reffou = '" + pom.getReference() + "' ), " + pom.getQte() + " , " + pom.getPu_ht() + ", '" + kapiece + "', 'stomag','vendu', getnum('LCK')),";

                ar.add(requeteCorps);
            }

        }

    }

只需将上述代码放入main方法并执行即可。输出将为1635.90,然后检查代码。

值1 635,90可能来自特定于区域设置的格式,并且该空格实际上是一个非中断空格,\u00A0。这样做通常是为了防止在可变宽度文本表示法中,数字内部发生换行

s = s.replace("\u00A0", "");

请提供一个问题,寻求调试帮助。为什么这段代码不起作用?必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参见:如何创建。使用编辑链接改进您的问题-不要通过评论添加更多信息。谢谢我将数据格式化为类的setter,这不是一个好主意。格式化应该由视图来完成,而不是由setter来完成。您正在做很多事情,但我们只需要:输入示例、预期输出和实际得到的内容。此外:阅读java命名约定。例如,在某些常量中只使用u。请注意:这里的javadoc是毫无意义的。这只是线路噪音——当然,这是在撒谎!您的方法不仅是设置一个值,还可以格式化任何输入的内容。与其说它是formatAndSet或类似的东西,我无法复制。你的两次尝试都能在我的电脑上运行,并在没有任何空间的情况下产生1635.90。工作起来很有魅力!非常感谢。有趣的是,对于regex\s,不间断空格不算作空白。我可以确认它没有,所以有必要在回答中的代码中明确提到它。@OleV.V。是的,古雅。可以解释为一个将两个文本块粘合成一个文本:在物理学中:一个反空间。
s = s.replace("\u00A0", "");