r.r
r.r

Reputation: 7153

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

Is it possible to save value of txtSearche in array splitted into seperate words?

txtSearche = "put returns between paragraphs";

something like this:

 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 correct?

Upvotes: 9

Views: 36264

Answers (7)

Dipankar Nalui
Dipankar Nalui

Reputation: 1261

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

Upvotes: 0

Boyan
Boyan

Reputation: 456

If you want a more complete solution and don't exactly worry about performance, you can have this one-liner take care of punctuation, etc. and give you an array of only the words.

string[] words = Regex.Replace(Regex.Replace(text, "[^a-zA-Z0-9 ]", " "), @"\s+", " ").Split(' ');

Upvotes: 1

Iain Ward
Iain Ward

Reputation: 9936

Yes try this:

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

which will give you:

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

EDIT: Also as Adkins mentions below, the words array will be created to whatever size is needed by the string that is provided. If you want the list to have a dynamic size I would say drop the array into a list using List wordList = words.ToList();

EDIT: Nakul to split by one space or more, just add them as parameters into the Split() method like below:

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

or you can tell it simply to split by a single space and ignore entries that are blank, caused by consecutive spaces, by using the StringSplitOptions.RemoveEmptyEntries enum like so

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

Upvotes: 22

user372724
user372724

Reputation:

StringBuilder sb = new StringBuilder(txtSearche); 

var result  =  sb.Tostring().Split(' '); 

Upvotes: 1

Aliostad
Aliostad

Reputation: 81660

None of above work with multiple spaces or new line!!!

Here is what works with them:

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

If you need to remove ellipsis then more processing is required and i can give you that as well.

UPDATE

In fact this is better:

 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);
  }

Outputs:

hi I am a wonderful56 text yeah

Upvotes: 2

Matt
Matt

Reputation: 2790

Below example will split the string into an array with each word as an item...

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

You can find more details here

Upvotes: 2

Steven Sudit
Steven Sudit

Reputation: 19620

You could use String.Split.

Upvotes: 4

Related Questions