How do I retrieve the first word from a string?

I’m trying to read just the first word from a string, but I can’t get it to work; my code won’t even compile. I’ve been looking on the MSDN for how to do this with c#, but I seem to be getting something screwed up because it isn’t working.

I thought I could use mystring.Split(’ ‘); but that returns the error: Cannot implicitly convert type string[ ]' to string’
Nothing seems to be working.

If you have an array of strings (which is what string [ ] type would be), you have to iterate through the strings to get “words” out. So maybe your storing them that way, and you could do myStringArray [0] = (string)firstWordFromStringArray;

But then again I’m guessing based on your error message.

string firstWord = myString.Split(new char[] { ' ' })[0];
3 Likes

Hmmm ok I see what your going for - and… I’m not too familiar with string manipulation, so somebody else will have to help ya out.

Sorry! Good luck!

Edit: like him^ haha

Thanks, Kelso!

Also, I got that to work as just string firstWord = myString.Split(' ')[0];

2 Likes