0

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

1
  • 2
    you must TRY first before asking! Commented Aug 17, 2012 at 17:35

2 Answers 2

3

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];
Sign up to request clarification or add additional context in comments.

Comments

0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.