2011-10-21 10 views
5

私はC#の分割文字列、空文字列も文字列と見なされる空の文字列を破棄する方法

"Two; [email protected];" 

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

として入力を入力するテキストボックスを持っているので、結果は3つの文字列が必要です。 1. 2つの 2. [email protected] 3. ""(空白)はあるからです。電子メールの後に、それは文字列として、どのように私はそれがかかる3番目の文字列を捨てることができると考えています。電子メールとセミコロンを入力してスペースバーを押すと、エラーが発生します。セミコロンの後にスペースがあれば、分割はそれを破棄する必要があります。

答えて

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

1を使用することができます – xanatos

+0

:-)動作しますRemoveEmptyEntriesもスペースを削除していますか?私はそれがString.Emptyを削除するためだけだと思った...それをテストしていない。 –

3

パスStringSplitOptionsパラメータ

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

コールと同じ方法で、...あなたを助けるべきそれは空のstrinを破棄する意味があるだろうとにかく最後からだけでなく、結果からgs。このような場合は、

char[] separators = new char[]{';'}; 
string[] result = txt_to.Text.Split(separators , StringSplitOptions.RemoveEmptyEntries); 
2

追加

string[] result = txt_to.Text.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries); 
0
string s=txt_to.Text; 
s = s.Replace(" ", ""); 
string[] result = s.Split(';'); 
関連する問題