Reputation: 2621
I have to split a string into two string in C#. Any text that appears before the first space as one and anything after the other as one.
For example 415 Wall St
415 as one string and Wall St as another string.
14-15 Broadway St
14-15 as one string and Broadway St as another string.
Is there any Regex in C#?
Thanks in advance
Upvotes: 0
Views: 414
Reputation: 3744
String has a Split method there, you don't need regular expressions for that. And that question has been asked before How can i split the string only once using C#
So in your case that would look like
String[] parts = s.Split(new char[] { ' ' }, 2);
String before = parts[0];
String after = parts[1];
Upvotes: 3
Reputation: 32827
string s="14-15 Broadway St";
Regex r=new Regex(@"(^.*?)\s+(.*?$)");
Match m=r.Match(s);
Console.WriteLine(m.Groups[1].Value);//14-15
Console.WriteLine(m.Groups[2].Value);//Broadway St
Upvotes: 0