Find and extract a number from a string

前端 未结 29 2628
温柔的废话
温柔的废话 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:55

    Regex.Split can extract numbers from strings. You get all the numbers that are found in a string.

    string input = "There are 4 numbers in this string: 40, 30, and 10.";
    // Split on one or more non-digit characters.
    string[] numbers = Regex.Split(input, @"\D+");
    foreach (string value in numbers)
    {
        if (!string.IsNullOrEmpty(value))
        {
        int i = int.Parse(value);
        Console.WriteLine("Number: {0}", i);
        }
    }
    

    Output:

    Number: 4 Number: 40 Number: 30 Number: 10

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

    Another simple solution using Regex You should need to use this

    using System.Text.RegularExpressions;
    

    and the code is

    string var = "Hello3453232wor705Ld";
    string mystr = Regex.Replace(var, @"\d", "");
    string mynumber = Regex.Replace(var, @"\D", "");
    Console.WriteLine(mystr);
    Console.WriteLine(mynumber);
    
    0 讨论(0)
  • 2020-11-22 03:57

    Here is my Algorithm

        //Fast, C Language friendly
        public static int GetNumber(string Text)
        {
            int val = 0;
            for(int i = 0; i < Text.Length; i++)
            {
                char c = Text[i];
                if (c >= '0' && c <= '9')
                {
                    val *= 10;
                    //(ASCII code reference)
                    val += c - 48;
                }
            }
            return val;
        }
    
    0 讨论(0)
  • 2020-11-22 03:57

    You will have to use Regex as \d+

    \d matches digits in the given string.

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

    An interesting approach is provided here by Ahmad Mageed, uses Regex and StringBuilder to extract the integers in the order in which they appear in the string.

    An example using Regex.Split based on the post by Ahmad Mageed is as follows:

    var dateText = "MARCH-14-Tue";
    string splitPattern = @"[^\d]";
    string[] result = Regex.Split(dateText, splitPattern);
    var finalresult = string.Join("", result.Where(e => !String.IsNullOrEmpty(e)));
    int DayDateInt = 0;
    
    int.TryParse(finalresult, out DayDateInt);
    
    0 讨论(0)
  • 2020-11-22 03:58

    Based on the last sample I created a method:

    private string GetNumberFromString(string sLongString, int iLimitNumbers)
    {
        string sReturn = "NA";
        int iNumbersCounter = 0;
        int iCharCounter = 0; 
    
        string sAlphaChars = string.Empty;
        string sNumbers = string.Empty;
        foreach (char str in sLongString)
        {
            if (char.IsDigit(str))
            {
                sNumbers += str.ToString();
                iNumbersCounter++;
                if (iNumbersCounter == iLimitNumbers)
                {
                    return sReturn = sNumbers;
                }
            }
            else
            {
                sAlphaChars += str.ToString();
                iCharCounter++;
                // reset the counter 
                iNumbersCounter = 0; 
            }
        }
        return sReturn;
    }
    
    0 讨论(0)
提交回复
热议问题