How to split text value into an array with words in C #?

Is it possible to store the value txtSearchein an array divided into separate words?

txtSearche = "put returns between paragraphs";

something like that:

 StringBuilder sb = new StringBuilder(txtSearche);

array1 = sb[1]   = put
array2 = sb[2]   = returns
array3 = sb[3]
array4 = sb[4]
array5 = sb[5]

how to do it right?

+5
source share
7 answers

Yes, try the following:

string[] words = txtSearche.Split(' ');

which will give you:

words[0]   = put
words[1]   = returns
words[2]   = between
words[3]   = paragraphs

EDIT: Also, as mentioned below by Adkins , an array of words will be created for any size needed for the string that is provided. If you want the list to have dynamic size, I would say that it will move the array to the list using List wordList = words.ToList ();

: Nakul , Split(), :

txtSearche.Split(new string[] { " ", "  ", "   " }, StringSplitOptions.None);

, , , StringSplitOptions.RemoveEmptyEntries,

txtSearche.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
+18
+4

...

string [] words = txtSearche.Split('');

+2

!!!

:

 string text = "hi!\r\nI am     a wonderful56 text... \r\nyeah...";
 string[] words =Regex.Split(text, @"\s+", RegexOptions.Singleline);

, , .

UPDATE

:

 string text = "hi!\r\nI am     a wonderful56 text... \r\nyeah...";
 MatchCollection matches = Regex.Matches(text, @"[\w\d_]+", RegexOptions.Singleline);
 foreach (Match match in matches)
 {
   if(match.Success)
      Console.WriteLine(match.Value);
  }

wonderful56

+2
source
StringBuilder sb = new StringBuilder(txtSearche); 

var result  =  sb.Tostring().Split(' '); 
+1
source

If you want a more complete solution and not worry about performance, you can use this one-liner for punctuation, etc. and give you an array of words only.

string[] words = Regex.Replace(Regex.Replace(text, "[^a-zA-Z0-9 ]", " "), @"\s+", " ").Split(' ');
0
source
private void button1_Click(object sender, EventArgs e)
{
    string s = textBox1.Text;            
    string[] words = s.Split(' ');           
    textBox2.Text = words[0];
    textBox3.Text = words[1];
}
0
source

All Articles