从字符串 C# 中删除点字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10298940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove dot character from a String C#
提问by Pinchy
Assume I have a string "2.36" and I want it trimmed to "236"
假设我有一个字符串“2.36”,我想把它修剪为“236”
I used Trim function in example
我在示例中使用了 Trim 功能
String amount = "2.36";
String trimmedAmount = amount.Trim('.');
The value of trimmedAmount is still 2.36
trimmedAmount 的值仍然是 2.36
When amount.Trim('6');it works perfectly but with '.'
当amount.Trim('6');它完美运行但带有 '.' 时
What I am doing wrong?
我做错了什么?
Thanks a lot Cheers
非常感谢干杯
采纳答案by Oded
回答by Ste
String.Trimremoves leading and trailing whitespace. You need to use String.Replace()
String.Trim删除前导和尾随空格。你需要使用String.Replace()
Like:
喜欢:
string amount = "2.36";
string newAmount = amount.Replace(".", "");
回答by ykatchou
Two ways :
两种方式:
string sRaw = "5.32";
string sClean = sRaw.Replace(".", "");
Trim is make for removing leading and trailings characters (such as space by default).
Trim 用于删除前导和尾随字符(例如默认情况下的空格)。
回答by Guffa
If you want to remove everything but the digits:
如果要删除除数字以外的所有内容:
String trimmedAmount = new String(amount.Where(Char.IsDigit).ToArray());
or:
或者:
String trimmedAmount = Regex.Replace(amount, @"\D+", String.Empty);

