简体   繁体   English

如何在Java字符串中的每个“,”之前输入“。”两个空格

[英]How do I enter a “.” 2 spaces before every “,” in a Java string

I've got a string in my Java project which looks something like this 我的Java项目中有一个字符串,看起来像这样
9201,92710,94500,920,1002

How can I enter a dot 2 places before the comma? 如何在逗号前2个地方输入点? So it looks like this: 所以看起来像这样:
920.1,9271.0,9450.0,92.0,100.2

I had an attempt at it but I can't get the last number to get a dot. 我曾尝试过,但无法获得最后一个数字来得到一个点。

numbers = numbers.replaceAll("([0-9],)", "\\.$1");

The result I got is 我得到的结果是
920.1,9271.0,9450.0,92.0,1002

Note: The length of the string is not always the same. 注意:字符串的长度并不总是相同。 It can be longer / shorter. 它可以更长或更短。

  1. Check if string ends with ",". 检查字符串是否以“,”结尾。 If not, append a "," to the string, run the same replaceAll, remove "," from end of String. 如果不是,请在字符串后附加“,”,然后运行相同的replaceAll,从字符串末尾删除“,”。
  2. Split string by the "," delimiter, process each piece adding the "." 用“,”分隔符分割字符串,处理每一个加“。”的部分。 where needed. 在需要的地方。
  3. Just add a "." 只需添加一个“。” at numbers.length-1 to solve the issue with the last number 在numbers.length-1上解决最后一个数字的问题

As your problem is not only inserting the dot before every comma , but also before end of string , you just must add this additional condition to your capturing group: 因为您的问题不仅是在每个逗号之前插入点,而且还在字符串的末尾 之前插入点,所以您只需要将此附加条件添加到捕获组中即可:

numbers = numbers.replaceAll("([0-9](,|$))", "\\.$1");

As suggested by Siguza, you could as well use a non-capturing group which is even more what a "human" would expect to be captured in the capturing group: 正如Siguza所建议的,您也可以使用一个非捕获组 ,它甚至比捕获组中的“人类”所期望的还要多:

numbers = numbers.replaceAll("([0-9](?:,|$))", "\\.$1");

But as a non-capturing group is (although a really nice feature) not standard Regex and the overhead is not that significant here, I would recommend using the first option. 但是,由于非捕获组不是标准Regex(尽管这是一个非常不错的功能),并且开销在这里并不重要,所以我建议使用第一个选项。

您可以使用单词边界:

numbers = numbers.replaceAll("(\\d)\b", ".$1");

Your solution is fine, as long as you put a comma at the end like dan said. 只要您像dan所说的那样在末尾加上逗号,您的解决方案就可以了。 So instead of: 所以代替:

numbers = numbers.replaceAll("([0-9],)", "\\.$1");

write: 写:

numbers = (numbers+",").replaceAll("([0-9],)", "\\.$1");
numbers = numbers.substring(0,numbers.size()-1);

You may use a positive lookahead to check for the , or end of string right after a digit and a zeroth backreference to the whole match: 您可以使用正向超前查询来检查整个匹配项的数字和第零向后引用之后的,或字符串的结尾:

String s = "9201,92710,94500,920,1002";
System.out.println(s.replaceAll("\\d(?=,|$)", ".$0"));
// => 920.1,9271.0,9450.0,92.0,100.2

See the Java demo and a regex demo . 请参阅Java演示regex演示

Details : 详细资料

  • \\\\d - exactly 1 digit... \\\\d正好1位数...
  • (?=,|$) - that must be before a , or end of string ( $ ). (?=,|$) -必须在字符串,或字符串( $ )的末尾。

A capturing variation ( Java demo ): 捕获版本( Java演示 ):

String s = "9201,92710,94500,920,1002";
System.out.println(s.replaceAll("(\\d)(,|$)", ".$1$2"));

You where right to go for the replaceAll method. 您在哪里可以使用replaceAll方法。 But your regex was not matching the end of the string, the last set of numbers. 但是您的正则表达式与字符串的末尾(最后一组数字)不匹配。

Here is my take on your problem: 这是我对您的问题的看法:

  public static void main(String[] args) {
    String numbers = "9201,92710,94500,920,1002";
    System.out.println(numbers.replaceAll("(\\d,|\\d$)", ".$1"));
  }

the regex (\\\\d,|\\\\d$) matches a digit followed by a comma \\d, , OR | 正则表达式(\\\\d,|\\\\d$)匹配一个数字,后跟一个逗号\\d, ,或| a digit followed by the end of the string \\d$ . 一个数字,后跟字符串\\d$的末尾。 I have tested it and found to work. 我已经对其进行了测试,发现可以正常工作。

As others have suggested you could add a comma at the end, run the replace all and then remove it. 正如其他人建议的那样,您可以在末尾添加逗号,运行全部替换,然后将其删除。 But it seems as extra effort. 但这似乎是额外的努力。 Example: 例:

  public static void main(String[] args) {
    String numbers = "9201,92710,94500,920,1002";
    //add on the comma
    numbers += ",";

    numbers = numbers.replaceAll("(\\d,)", "\\.$1");

    //remove the comma
    numbers = numbers.substring(0, numbers.length()-1);
    System.out.println(numbers);
  }

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

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