String parsing, extracting numbers and letters

后端 未结 6 2125
逝去的感伤
逝去的感伤 2020-12-30 15:19

What\'s the easiest way to parse a string and extract a number and a letter? I have string that can be in the following format (number|letter or letter|number), i.e \"10A\",

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-30 15:42

    char letter = str.Single(c => char.IsLetter(c));
    int num = int.Parse(new string(str.Where(c => char.IsDigit(c)).ToArray()));
    

    This solution is not terribly strict (it would allow things like "5A2" and return 'A' and 52) but it may be fine for your purposes.

提交回复
热议问题