Split a string in C #, empty space is also considered as a string, how to reset an empty string

I have a text box in which I enter the input as

"Two; abc@kk.com;"

string[] result = txt_to.Text.Split(';');

so what happens here is three lines. 1. two 2. abc@kk.com 3. "" (empty space) since a exists; after email, he thinks that as a string, I can discard the third line that it accepts. This happens when I enter the email address and semicolon and press the space bar, which causes an error. If it is just a space after the semicolon, then the split should discard it, how to do it

+5
source share
6 answers

, "" ( )? ...

string[] result = txt_to.Text.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
+12
var arr = mystring.Split(new string[]{";"}, StringSplitOptions.RemoveEmptyEntries);
+6

Pass StringSplitOptions parameter

var result = yourString.Split(new string[] {";"}, StringSplitOptions.RemoveEmptyEntries);
+3
source

Call the same method by adding StringSplitOptions.RemoveEmptyEntries

http://msdn.microsoft.com/it-it/library/tabh47cf.aspx

+2
source

It seems to me that it makes sense to discard empty lines from the result anyway, not just at the end. If so, you can use

char[] separators = new char[]{';'};
string[] result = txt_to.Text.Split(separators , StringSplitOptions.RemoveEmptyEntries);
+2
source
string s=txt_to.Text;
s = s.Replace(" ", "");
string[] result = s.Split(';');
0
source

All Articles