I have a string which contains words. Example: string s = " asd qwert 123 "; How can I read asd qwert and 123 from the string seperately with a loop in c#? Thanx in advance
Asked
Active
Viewed 72 times
-2
-
just split the string by space. – Avinash Raj Jul 29 '15 at 17:05
-
@Shaharyar The OP wants to split on space, not a multi-character delimiter. I'm sure there's a dup for this, but that's not the right one. – juharr Jul 29 '15 at 17:08
-
`String[] words = s.Split(' ', StringSplitOptions.RemoveEmptyEntries);` then itterate by `words` – Dmitry Bychenko Jul 29 '15 at 17:14
1 Answers
2
If you make use of the Split
method you can do so quite easily:
foreach(var str in s.Split(' '))
{
// str would be one of asd, qwert and 123
// since splitting on whitespace gives you an array
// with the words in the string s, which are separated one
// from the other with a whitespace between them.
}
Please have a look here.

Christos
- 53,228
- 8
- 76
- 108
-
1The OP might need to use the overload that takes a `StringSplitOptions` and pass in `StringSplitOptions.RemoveEmptyEntries` – juharr Jul 29 '15 at 17:10