Find and extract a number from a string

前端 未结 29 2739
温柔的废话
温柔的废话 2020-11-22 03:19

I have a requirement to find and extract a number contained within a string.

For example, from these strings:

string test = \"1 test\"
string test1 =         


        
相关标签:
29条回答
  • 2020-11-22 03:53

    go through the string and use Char.IsDigit

    string a = "str123";
    string b = string.Empty;
    int val;
    
    for (int i=0; i< a.Length; i++)
    {
        if (Char.IsDigit(a[i]))
            b += a[i];
    }
    
    if (b.Length>0)
        val = int.Parse(b);
    
    0 讨论(0)
  • 2020-11-22 03:53

    The question doesn't explicitly state that you just want the characters 0 to 9 but it wouldn't be a stretch to believe that is true from your example set and comments. So here is the code that does that.

            string digitsOnly = String.Empty;
            foreach (char c in s)
            {
                // Do not use IsDigit as it will include more than the characters 0 through to 9
                if (c >= '0' && c <= '9') digitsOnly += c;
            }
    

    Why you don't want to use Char.IsDigit() - Numbers include characters such as fractions, subscripts, superscripts, Roman numerals, currency numerators, encircled numbers, and script-specific digits.

    0 讨论(0)
  • 2020-11-22 03:53

    Using @tim-pietzcker answer from above, the following will work for PowerShell.

    PS C:\> $str = '1 test'
    PS C:\> [regex]::match($str,'\d+').value
    1
    
    0 讨论(0)
  • 2020-11-22 03:54

    For those who want decimal number from a string with Regex in TWO line:

    decimal result = 0;
    decimal.TryParse(Regex.Match(s, @"\d+").Value, out result);
    

    Same thing applys to float, long, etc...

    0 讨论(0)
  • 2020-11-22 03:54

    Extension method to get all positive numbers contained in a string:

        public static List<long> Numbers(this string str)
        {
            var nums = new List<long>();
            var start = -1;
            for (int i = 0; i < str.Length; i++)
            {
                if (start < 0 && Char.IsDigit(str[i]))
                {
                    start = i;
                }
                else if (start >= 0 && !Char.IsDigit(str[i]))
                {
                    nums.Add(long.Parse(str.Substring(start, i - start)));
                    start = -1;
                }
            }
            if (start >= 0)
                nums.Add(long.Parse(str.Substring(start, str.Length - start)));
            return nums;
        }
    

    If you want negative numbers as well simply modify this code to handle the minus sign (-)

    Given this input:

    "I was born in 1989, 27 years ago from now (2016)"
    

    The resulting numbers list will be:

    [1989, 27, 2016]
    
    0 讨论(0)
  • 2020-11-22 03:55

    What I use to get Phone Numbers without any punctuation...

    var phone = "(787) 763-6511";
    
    string.Join("", phone.ToCharArray().Where(Char.IsDigit));
    
    // result: 7877636511
    
    0 讨论(0)
提交回复
热议问题