简体   繁体   English

字符串替换 - C#

[英]string replacing - C#

Say I have a string containing numbers and other chars. 假设我有一个包含数字和其他字符的字符串。

I want to reduce the string to numbers only. 我想将字符串减少到只有数字。

Fe from 23232-2222-d23231 to 23232222223231 铁从23232-2222-d23231到23232222223231

Can this be done with string.replace()? 可以用string.replace()完成吗?

if not, what's the simplest and shortest way? 如果没有,最简单和最简单的方法是什么?

10x! 10倍!

There are a bunch of possibilities, from Regular Expressions to handling the text yourself. 从正则表达到自己处理文本,有很多可能性。 I'd go for this: 我会这样做:

Regex.Replace(input, @"\D+", "")

Well, you will get about 874355876234857 answers with String.Replace and Regex.Replace, so here's a LINQ solution: 那么,你将通过String.Replace和Regex.Replace得到大约874355876234857的答案,所以这是一个LINQ解决方案:

code = new String((from c in code where Char.IsDigit(c) select c).ToArray());

Or using extension methods: 或者使用扩展方法:

code = new String(code.Where(c => Char.IsDigit(c)).ToArray());

是的,您可以使用String.replace,但使用正则表达式会更明智,这样您就可以轻松地匹配更多标准。

The best way would be to user Regular Expressions. 最好的方法是使用正则表达式。 You example would be: 你的例子是:

RegEx.Replace("23232-2222-d23231", "\\D+", "");

Regular expressions, as sampled, are the simplest and shortest. 采样的正则表达式是最简单和最短的。

I wonder if below would be faster? 我想知道下面会更快吗?

            string sample = "23232-2222-d23231";
            StringBuilder resultBuilder = new StringBuilder(sample.Length);
            char c;
            for (int i = 0; i < sample.Length; i++)
            {
                c = sample[i];
                if (c >= '0' && c <= '9')
                {
                    resultBuilder.Append(c);
                }
            }
            Console.WriteLine(resultBuilder.ToString());
            Console.ReadLine();

guessing it would depend on a few things, including string length. 猜测它取决于一些事情,包括字符串长度。

The simplest would be to use Replace. 最简单的方法是使用Replace。

 string test = "23232-2222-d23231";
 string newString = test.Replace("-","").Replace("d","");

But using REGEX would be better, but tougher. 但使用REGEX会更好,但更难。

It is not (easily) possible with string.Replace() . string.Replace()不能(轻松)实现。 The simplest solution is the following function/code: 最简单的解决方案是以下功能/代码:

public string GetDigits(string input)
{
    Regex r = new Regex("[^0-9]+");
    return r.Replace(input, "");
}

I would use a regular expression. 我会使用正则表达式。

See this post Regex for numbers only 请参阅此文章正则表达式仅限数字

You can use a simple extension method: 您可以使用简单的扩展方法:

    public static string OnlyDigits(this string s)
    {
        if (s == null)
            throw new ArgumentNullException("null string");

        StringBuilder sb = new StringBuilder(s.Length);
        foreach (var c in s)
        {
            if (char.IsDigit(c))
                sb.Append(c);
        }
        return sb.ToString();
    }

Try 尝试

string str="23232-2222-d23231";
str=Regex.Replace(str, @"\D+", String.Empty);

您可以使用LINQ:

string allDigits = new String(input.Where(c => Char.IsDigit(c)).ToArray());

You can use a regular expression. 您可以使用正则表达式。

string str = "sdfsdf99393sdfsd";
str = Regex.Replace(str, @"[a-zA-Z _\-]", "");

I've used this to return only numbers in a string before. 我之前用它来只返回字符串中的数字。

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

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