如何在asp.net c#中提取字符直到在子字符串中找到数字
本文关键字:数字 字符串 提取 asp net 字符 | 更新日期: 2023-09-27 18:29:47
如何提取字符,直到在asp.net c#中的子字符串中找到数字
我的字符串像
NR21380SR9956NER6754WR98765SR98700
一种简单的方法:
var firstLetterCharacters = text.TakeWhile(Char.IsLetter);
或者反过来:
var firstLetterCharacters = text.TakeWhile(c => !Char.IsDigit(c));
如果你需要一个新的字符串:
string newText = new string(firstLetterCharacters.ToArray());
您可以使用正则表达式来替换所有非字母:
string s=NR21380 SR9956 NER6754 WR98765 SR98700
string s2 = Regex.Replace(s, @"[^A-Z]+", String.Empty);
我只是试着猜测输出:
string s = "NR21380 SR9956 NER6754 WR98765 SR98700";
var list = s.Split()
.Select(x => String.Join("", x.TakeWhile(c => char.IsLetter(c))))
.ToList();
输出将是NR SR NER WR SR
的列表
尝试这个
String aa = "NR21380 SR9956 NER6754 WR98765 SR98700";
//getting the first chars from the dummy string.
var firstChars= Regex.Match(aa, @"[A-Z]+");