Stripping out non-numeric characters in string

后端 未结 11 582
南旧
南旧 2021-01-30 09:46

Hey Im looking to strip out non-numeric characters in a string in ASP.NET C#

So i.e 40,595 p.a.

would end up with 40595

Thanks

相关标签:
11条回答
  • 2021-01-30 10:15

    An extension method will be a better approach:

    public static string GetNumbers(this string text)
        {
            text = text ?? string.Empty;
            return new string(text.Where(p => char.IsDigit(p)).ToArray());
        }
    
    0 讨论(0)
  • 2021-01-30 10:15

    The accepted answer is great, however it doesn't take NULL values into account, thus making it unusable in most scenarios.

    This drove me into using these helper methods instead. The first one answers the OP, while the others may be useful for those who want to perform the opposite:

        /// <summary>
        /// Strips out non-numeric characters in string, returning only digits
        /// ref.: https://stackoverflow.com/questions/3977497/stripping-out-non-numeric-characters-in-string
        /// </summary>
        /// <param name="input">the input string</param>
        /// <param name="throwExceptionIfNull">if set to TRUE it will throw an exception if the input string is null, otherwise it will return null as well.</param>
        /// <returns>the input string numeric part: for example, if input is "XYZ1234A5U6" it will return "123456"</returns>
        public static string GetNumbers(string input, bool throwExceptionIfNull = false)
        {
            return (input == null && !throwExceptionIfNull) 
                ? input 
                : new string(input.Where(c => char.IsDigit(c)).ToArray());
        }
    
        /// <summary>
        /// Strips out numeric and special characters in string, returning only letters
        /// </summary>
        /// <param name="input">the input string</param>
        /// <param name="throwExceptionIfNull">if set to TRUE it will throw an exception if the input string is null, otherwise it will return null as well.</param>
        /// <returns>the letters contained within the input string: for example, if input is "XYZ1234A5U6~()" it will return "XYZAU"</returns>
        public static string GetLetters(string input, bool throwExceptionIfNull = false)
        {
            return (input == null && !throwExceptionIfNull) 
                ? input 
                : new string(input.Where(c => char.IsLetter(c)).ToArray());
        }
    
        /// <summary>
        /// Strips out any non-numeric/non-digit character in string, returning only letters and numbers
        /// </summary>
        /// <param name="input">the input string</param>
        /// <param name="throwExceptionIfNull">if set to TRUE it will throw an exception if the input string is null, otherwise it will return null as well.</param>
        /// <returns>the letters contained within the input string: for example, if input is "XYZ1234A5U6~()" it will return "XYZ1234A5U6"</returns>
        public static string GetLettersAndNumbers(string input, bool throwExceptionIfNull = false)
        {
            return (input == null && !throwExceptionIfNull) 
                ? input 
                : new string(input.Where(c => char.IsLetterOrDigit(c)).ToArray());
        }
    

    For additional info, read this post on my blog.

    0 讨论(0)
  • 2021-01-30 10:19

    Well, you know what the digits are: 0123456789, right? Traverse your string character-by-character; if the character is a digit tack it onto the end of a temp string, otherwise ignore. There may be other helper methods available for C# strings but this is a generic approach that works everywhere.

    0 讨论(0)
  • 2021-01-30 10:19
     var output = new string(input.Where(char.IsNumber).ToArray());
    
    0 讨论(0)
  • 2021-01-30 10:20

    Another option ...

    private static string RemoveNonNumberDigitsAndCharacters(string text)
    {
        var numericChars = "0123456789,.".ToCharArray();
        return new String(text.Where(c => numericChars.Any(n => n == c)).ToArray());
    }
    
    0 讨论(0)
  • 2021-01-30 10:23

    Use either a regular expression that's only capturing 0-9 and throws away the rest. A regular expression is an operation that's going to cost a lot the first time though. Or do something like this:

    var sb = new StringBuilder();
    var goodChars = "0123456789".ToCharArray();
    var input = "40,595";
    foreach(var c in input)
    {
      if(goodChars.IndexOf(c) >= 0)
        sb.Append(c);
    }
    var output = sb.ToString();
    

    Something like that I think, I haven't compiled though..

    LINQ is, as Fredrik said, also an option

    0 讨论(0)
提交回复
热议问题