Extract number at end of string in C#

前端 未结 9 2396
無奈伤痛
無奈伤痛 2020-12-17 08:03

Probably over analysing this a little bit but how would stackoverflow suggest is the best way to return an integer that is contained at the end of a string.

Thus far

9条回答
  •  -上瘾入骨i
    2020-12-17 08:42

    Am I allowed to go crazy with this?

    using System.Text;
    using System.Linq;
    
    static string GetNum(string input)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = input.Length - 1; i >= 0; i--)
        {
            if (input[i] < 48 || input[i] > 57)
                break;
    
            sb.Append(input[i]);
        }
    
        return String.Concat(sb.ToString().ToCharArray().Reverse());
    }
    

提交回复
热议问题