简体   繁体   English

C# 如何根据第一个数字拆分字符串?

[英]C# How can i split a string on first number?

I have the following string "ABCD EFG 201 E" i want to split it on the first number that it founds a digit and return both strings Ex.我有以下字符串"ABCD EFG 201 E"我想在它找到数字的第一个数字上拆分它并返回两个字符串 Ex。 "ABCD EFG" and "201 E" "ABCD EFG""201 E"

i tried Regex.Split and other stuff but i don't get it.我尝试了 Regex.Split 和其他东西,但我不明白。 can someone help me please?有谁可以帮助我吗? Thanks, Best Regards.谢谢,最好的问候。

not using regex cos I dont really like them - story goes "you have a problem, you decide to use regex on it, you now have 2 problems"不使用正则表达式,因为我真的不喜欢它们 - 故事是“你有问题,你决定在上面使用正则表达式,你现在有 2 个问题”

 char[] digits = {'0','1','2','3','4','5','6','7','8','9'};
 string s = "ABCD EFG 201 E";
 var idx = s.IndexOfAny(digits);
 if (idx !=-1){
     var first = s.Substring(0,idx);
     var second = s.Substring(idx);
 }

Using Regex , you can do it like below:使用Regex ,你可以像下面这样:

    string Text = "ABCD EFG 201 E";  
    string[] digits = Regex.Split(Text, @"(\d.*)");  
    foreach (string value in digits)  
        Console.WriteLine(value); 

Code Here代码在这里

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

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