简体   繁体   English

字符串解析,提取数字和字母

[英]String parsing, extracting numbers and letters

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), ie "10A", "B5", "C10", "1G", etc. 我的字符串可以是以下格式(数字|字母或字母|数字),即“10A”,“B5”,“C10”,“1G”等。

I need to extract the 2 parts, ie "10A" -> "10" and "A". 我需要提取2个部分,即“10A” - >“10”和“A”。

Update: Thanks to everyone for all the excellent answers 更新:感谢大家提供的所有优秀答案

Easiest way is probably to use regular expressions. 最简单的方法可能是使用正则表达式。

((?<number>\d+)(?<letter>[a-zA-Z])|(?<letter>[a-zA-Z])(?<number>\d+))

You can then match it with your string and extract the value from the groups. 然后,您可以将其与字符串匹配,并从组中提取值。

Match match = regex.Match("10A");
string letter = match.Groups["letter"].Value;
int number = int.Parse(match.Groups["number"].Value);

The easiest and fastest is to use simple string operations. 最简单,最快速的是使用简单的字符串操作。 Use the IsDigit method to check where the letter is, and parse the rest of the string to a number: 使用IsDigit方法检查字母的位置,并将字符串的其余部分解析为数字:

char letter = str[0];
int index = 1;
if (Char.IsDigit(letter)) {
   letter = str[str.Length - 1];
   index = 0;
}
int number = int.Parse(str.Substring(index, str.Length - 1));
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. 这个解决方案并不是非常严格(它会允许像“5A2”这样的东西并返回'A'和52),但它可能适合你的目的。

Just to be different: 只是为了与众不同:

string number = input.Trim("ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray());
string letter = input.Trim("0123456789".ToCharArray());

Here is how I would approach this. 我将如何处理这个问题。 You can step through this and put gc1["letter"], gc1["number"], gc2["letter"], and gc2["number"] in the watch window to see that it worked (step just past the last line of code here, of course). 您可以单步执行此操作并在监视窗口中输入gc1 [“letter”],gc1 [“number”],gc2 [“letter”]和gc2 [“number”]以查看它是否有效(步骤刚刚过去)这里的代码行,当然)。

The regular epxression will take either pattern requiring one or more letter and number in each case. 常规表现将采用任何一种模式,在每种情况下都需要一个或多个字母和数字。

        Regex pattern = new Regex("^(?<letter>[a-zA-Z]+)(?<number>[0-9]+)|(?<number>[0-9]+)(?<letter>[a-zA-Z]+)$");
        string s1 = "12A";
        string s2 = "B45";
        Match m1 = pattern.Match(s1);
        Match m2 = pattern.Match(s2);
        GroupCollection gc1 = m1.Groups;
        GroupCollection gc2 = m2.Groups;

Using Sprache and some Linq kung-fu: 使用Sprache和一些Linq kung-fu:

var tagParser =
    from a in Parse.Number.Or(Parse.Letter.Once().Text())
    from b in Parse.Letter.Once().Text().Or(Parse.Number)
    select char.IsDigit(a[0]) ?
           new{Number=a, Letter=b} : new{Number=b, Letter=a};

var tag1 = tagParser.Parse("10A");
var tag2 = tagParser.Parse("A10");

tag1.Letter; // should be A 
tag1.Number; // should be 10

tag2.Letter; // should be A
tag2.Number; // should be 10

/* Output:
   A
   10
   A
   10
 */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM