Splitting a string
-
I was using the following code to split textbox text by a single space character:
string words = notesTextBox.Text;
string[] splitWords = words.Split(new Char[] { ' '});I now need to split the textbox text by either the space character or a carriage return. How can I do this please?
-
I was using the following code to split textbox text by a single space character:
string words = notesTextBox.Text;
string[] splitWords = words.Split(new Char[] { ' '});I now need to split the textbox text by either the space character or a carriage return. How can I do this please?
You can add chars to your array:
string words = notesTextBox.Text;
string[] splitWords = words.Split(new Char[] { ' ', '\r' });2+2=5 for very large amounts of 2 (always loved that one hehe!)
-
I was using the following code to split textbox text by a single space character:
string words = notesTextBox.Text;
string[] splitWords = words.Split(new Char[] { ' '});I now need to split the textbox text by either the space character or a carriage return. How can I do this please?
If you need to split the text by a carriage return and/or space character, you can use the following code sample:
string words = notesTextBox.Text;
words = words.Replace("\r\n", " ");
string[] splitWords = words.Split(new char[] { ' ' });This will replace all the carriage returns with spaces, then split the text at each space. If you do what Moreno said and just add the '\r' and '\n' characters to the array, you will end up getting more split words in your array. You will have to try each way and see which best fits your needs.
string words = notesTextBox.Text;
string[] splitWords = words.Split(new char[] { ' ', '\r', '\n' });modified on Monday, August 10, 2009 11:55 AM