使用Regex删除字符串末尾的所有零

时间:2022-03-19 04:53:24

I have to Remove Zero at the end of string only if string have , in it. Can i do all below using Regex. So

我必须在字符串末尾删除零,如果字符串有,则在其中。我可以使用Regex完成以下所有操作。所以

123,90 -> 123,9
123,00 -> 123,
123,0 -> 123,
123,0090 ->123,009
00,34-> 00,34
ABC -> ABC

4 个解决方案

#1


1  

Search using this regex:

使用此正则表达式搜索:

(,\d*?)0+\b

and replace it by:

并替换为:

$1

IdeOne Code demo

IdeOne Code演示

RegExStrorm Demo - Click on "Context" tab to see results

RegExStrorm演示 - 单击“上下文”选项卡以查看结果

RegEx101 Demo

#2


1  

Remember than RegEx is used to test a pattern. It will return true or false rather than perform any changes to the string itself.

请记住,RegEx用于测试模式。它将返回true或false,而不是对字符串本身执行任何更改。

If you want to search for characters at the end of a string, you can use the $ sign to build your regex

如果要在字符串末尾搜索字符,可以使用$符号构建正则表达式

enter image description here

在此处输入图像描述

Use this tool if you want to test this live

如果要进行实时测试,请使用此工具

So, this RegEx will return true each time you have a 0 at the end, false otherwise

因此,每次最后都有0时,此RegEx将返回true,否则返回false

0$

Now, if you need to remove that zero, you need to perform a TRIM

现在,如果您需要删除该零,则需要执行TRIM

"129,0".TrimEnd('0')

Note the quotes within the TrimEnd function. This is because this method expects a char an not a string.

注意TrimEnd函数中的引号。这是因为此方法需要char而不是字符串。

Regards! Rod

#3


0  

     String line = "120,90";


    String newLine = line.replaceAll("0*$", "");
//120,9

removes any '0' starting from the end of the string expression. Replaces all 0s with empty space from the end of the string up to the first non 0 character in the string.

从字符串表达式的末尾开始删除任何“0”。将所有0替换为字符串末尾的空格,直到字符串中的第一个非0字符。

#4


0  

It seems as if you use , as number-decimal-separator. So you could use double.TryParse and double.ToString to get the desired result:

看起来好像你使用数字小数分隔符。所以你可以使用double.TryParse和double.ToString来获得所需的结果:

string str = "123,0090";
double d;
if (double.TryParse(str, out d))
    str = d.ToString();

If you want to ensure that comma is used as decimal separator:

如果要确保将逗号用作小数分隔符:

if (double.TryParse(str, NumberStyles.Any, new CultureInfo("de-DE"), out d))
    str = d.ToString();

If you'd use decimal.TryParse instead the number of trailing zeros would be preserved which isn't desired in this case. Msdn: "The scaling factor also preserves any trailing zeros in a Decimal number. ....might be revealed by the ToString method...."

如果你使用decimal.TryParse,则会保留尾随零的数量,在这种情况下不需要。 Msdn:“缩放因子也保留了十进制数中的任何尾随零....可能会被ToString方法显示....”

#1


1  

Search using this regex:

使用此正则表达式搜索:

(,\d*?)0+\b

and replace it by:

并替换为:

$1

IdeOne Code demo

IdeOne Code演示

RegExStrorm Demo - Click on "Context" tab to see results

RegExStrorm演示 - 单击“上下文”选项卡以查看结果

RegEx101 Demo

#2


1  

Remember than RegEx is used to test a pattern. It will return true or false rather than perform any changes to the string itself.

请记住,RegEx用于测试模式。它将返回true或false,而不是对字符串本身执行任何更改。

If you want to search for characters at the end of a string, you can use the $ sign to build your regex

如果要在字符串末尾搜索字符,可以使用$符号构建正则表达式

enter image description here

在此处输入图像描述

Use this tool if you want to test this live

如果要进行实时测试,请使用此工具

So, this RegEx will return true each time you have a 0 at the end, false otherwise

因此,每次最后都有0时,此RegEx将返回true,否则返回false

0$

Now, if you need to remove that zero, you need to perform a TRIM

现在,如果您需要删除该零,则需要执行TRIM

"129,0".TrimEnd('0')

Note the quotes within the TrimEnd function. This is because this method expects a char an not a string.

注意TrimEnd函数中的引号。这是因为此方法需要char而不是字符串。

Regards! Rod

#3


0  

     String line = "120,90";


    String newLine = line.replaceAll("0*$", "");
//120,9

removes any '0' starting from the end of the string expression. Replaces all 0s with empty space from the end of the string up to the first non 0 character in the string.

从字符串表达式的末尾开始删除任何“0”。将所有0替换为字符串末尾的空格,直到字符串中的第一个非0字符。

#4


0  

It seems as if you use , as number-decimal-separator. So you could use double.TryParse and double.ToString to get the desired result:

看起来好像你使用数字小数分隔符。所以你可以使用double.TryParse和double.ToString来获得所需的结果:

string str = "123,0090";
double d;
if (double.TryParse(str, out d))
    str = d.ToString();

If you want to ensure that comma is used as decimal separator:

如果要确保将逗号用作小数分隔符:

if (double.TryParse(str, NumberStyles.Any, new CultureInfo("de-DE"), out d))
    str = d.ToString();

If you'd use decimal.TryParse instead the number of trailing zeros would be preserved which isn't desired in this case. Msdn: "The scaling factor also preserves any trailing zeros in a Decimal number. ....might be revealed by the ToString method...."

如果你使用decimal.TryParse,则会保留尾随零的数量,在这种情况下不需要。 Msdn:“缩放因子也保留了十进制数中的任何尾随零....可能会被ToString方法显示....”