Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a String into only 2 parts

Tags:

c#

.net

I want to take a string from a textbox (txtFrom) and save the first word and save whatever is left in another part. (the whatever is left is everything past the first space)

Example string = "Bob jones went to the store"

array[0] would give "Bob"
array[1] would give "jones went to the store"

I know there is string[] array = txtFrom.Split(' '); , but that gives me an array of 6 with individual words.

like image 325
John Avatar asked May 06 '11 03:05

John


People also ask

How do you split a string into two parts in Python?

Use Split () Function This function splits the string into smaller sections. This is the opposite of merging many strings into one. The split () function contains two parameters. In the first parameter, we pass the symbol that is used for the split.

How do you split a string in half?

We can use the len() method and get half of the string. We then use the slice notation technique to slice off the first and second of the string value to store the then in separate variables.

How do you split a string into two parts in Ruby?

Ruby – String split() Method with ExamplesIf pattern is a Regular Expression or a string, str is divided where the pattern matches. Parameters: arr is the list, str is the string, pattern is the either string or regExp, and limit is the maximum entries into the array. Returns: Array of strings based on the parameters.


1 Answers

Use String.Split(Char[], Int32) overload like this:

string[] array = txtFrom.Text.Split(new char[]{' '},2);

http://msdn.microsoft.com/en-us/library/c1bs0eda.aspx

like image 101
manojlds Avatar answered Sep 23 '22 11:09

manojlds