tags:

views:

149

answers:

5

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?

+4  A: 

You could use String.Split.

Steven Sudit
And, yes, there's also a `Join`.
Steven Sudit
+11  A: 

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);
w69rdy
It is also worth mentioning that 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<string> wordList = words.ToList<string>();
Adkins
@Adkins: Thanks I'll add it to the answer
w69rdy
I want to know what return when consecutive spaces in string
Nakul Chaudhary
@Nakul: See my update in above
w69rdy
@w69rdy nice answer +1
Nakul Chaudhary
+2  A: 

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

Matt
+1  A: 

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

Aliostad
That's not entirely true, `String.Split` has an overload that takes an array of split characters, and an option to skip empty entries, so you could use `text.Split(" \t\r\n\x85\xA0.,;:!?()-\"".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)`. If you're going to go all the way to regex though, `@"\W+(?<!')"` would be better than `@"\s+"`, since it would remove punctuation as well (the `(?<!')` is to protect contractions and possessives, with the unfortunate side effect that it won't remove single quotes).
P Daddy
+1  A: 
StringBuilder sb = new StringBuilder(txtSearche); 

var result  =  sb.Tostring().Split(' '); 
priyanka.sarkar_2
`new StringBuilder(txt).ToString()` = no-op
P Daddy