Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a string into 2 strings

I need help using the Split function in C#. The user needs to enter a 3 word phrase or more. I already know what the users first word will be. For example if the user enters 'Microsoft Visual Studio 2015', I already know the user will enter 'Microsoft'. How can I get Split to cut off the first word and give only the following phrase as a string?


1 Answers

char[] separators = { ' ' };
string secondword = myString.Split(separators, 2)[1];

Will give you the right output. The split statement with an int argument specifies that you want to return at most 2 substrings, and the array index [1] directs the second of those (whatever is after the first space) to be your output.

If you need the first word you can do similar:

char[] separators = { ' ' };
string firstword= myString.Split(separators, 2)[0];
like image 152
nhouser9 Avatar answered May 19 '26 17:05

nhouser9