Splitting number and string using linq

use*_*602 2 c# string-split

I have a string which its first part is a string and last one is a number, like this:

ahde7394

so what I would like to obtain is:

ahde

7394

I have thought to first extract the string and then from the last position of the character obtain the number until the end of the string so I think using indexers can be done somehow:

var stringQuery = NameComp.Select((str,index) => new {myStr=str, position=index})
                                               .Where(c => !Char.IsDigit(c.myStr)).Select((ch,pos) => new { newStr=ch, pos=pos} );
Run Code Online (Sandbox Code Playgroud)

then I could do:

1) To obtain the string: stringQuery.newStr

2) To obtain the number: stringQuery.Skip(stringQuery.pos).Select(d => d);

but it is not working, after obtaining stringQuery I cannot access to its items as it is an anonymous type....

Any ideas?

Solution using LINQ, guessing that str="ahde7394":

string letters = new string(str.TakeWhile(c => Char.IsLetter(c)).ToArray());
Run Code Online (Sandbox Code Playgroud)

and for number:

string number = new string(str.Skip(letters.Length).TakeWhile(c => Char.IsDigit(c)).ToArray());
Run Code Online (Sandbox Code Playgroud)

or better guessing last part is a number:

   string number = str.Substring(name.Length);
Run Code Online (Sandbox Code Playgroud)

Sim*_*ead 5

我同意 dtb 的观点,即 LINQ 可能不是正确的解决方案。

正则表达式是另一种选择,假设您的字符串可以比您提供的更可变。

var str = "ahde7394";

var regex = Regex.Match(str, @"([a-zA-Z]+)(\d+)");

var letters = regex.Groups[1].Value; // ahde
var numbers = regex.Groups[2].Value; // 7394
Run Code Online (Sandbox Code Playgroud)