Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to take a text as input in textbox1.
break the text into two parts.
Store 1st word in textbox2 and 2nd word in textbox3.
i tried this code. it stores the 2nd word in textBox2 and textBox3.
Please help me.

What I have tried:

C#
private void button1_Click(object sender, EventArgs e)
       {
           var input=textBox1.Text;
           var matches = Regex.Matches(input, @"\w+[^\s]*\w+|\w");
           foreach (Match match in matches)
           {
               var word = match.Value;
               textBox2.Text = word;
            }
           foreach (Match match in matches)
           {
               var word = match.Value;
               textBox3.Text = word;
           }
       }
Posted
Updated 26-Apr-16 18:38pm
Comments
VR Karthikeyan 27-Apr-16 0:28am    
How do you want split your string? You said you want split the string, but in your code you are just matching the pattern with string. So Just give an example input and output.
PIEBALDconsult 27-Apr-16 0:31am    
Don't use foreach, just use indexing.
And I don't think your RegEx does what you want.
Try testing what you have: http://www.codeproject.com/Articles/39165/RegexTester
https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx

Try this,
it works only if they enter two words.

C#
var input =   textBox1.Text;
         var matches = Regex.Matches(input, @"\w+[^\s]*\w+|\w");
         if (matches.Count == 2)
         {
             textBox2.Text = matches[0].Value;
             textBox3.Text = matches[1].Value;
         }
 
Share this answer
 
v2
got a solution. it works fine. thank u..

C#
private void button1_Click(object sender, EventArgs e)
       {
           string s = textBox1.Text;
           string[] words = s.Split(' ');
           textBox2.Text = words[0];
           textBox3.Text = words[1];

       }
 
Share this answer
 
Comments
PIEBALDconsult 27-Apr-16 0:51am    
Please don't answer your own question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900