2014-08-29 60 views
-1

我的下一个字符串如何通过一个字

string example="This is a string and i want to split by [this], it's posible?"; 

分割字符串有没有,让我如果发现某个词或字符串分割字符串的方法,如“[这]”或“拆分”

string splitter="[this]"; 
string1="This is a string and i want to split by "; 
string2="[this], it's posible?"; 

或其他例如:

string splitter="split"; 
    string1="This is a string and i want to split "; 
    string2="by [this], it's posible?"; 

什么是做到这一点的最好方法是什么?

+2

string.Split().. .........一个惊人的简单和快速搜​​索...... – 2014-08-29 04:04:54

+0

对于“最好的方式”的问题,请显示“劣等”为什么你现在拥有“更好”的具体标准。即对于性能,通常最好不要创建新的字符串,并使用自定义的索引子字符串。 – 2014-08-29 04:10:00

回答

1

您可以使用string.Split

string[] splitted = example.Split(new[] { splitter }, StringSplitOptions.None); 

如果你想在分裂的部分(在输入例如各地尤其是分路器)摆脱whitechars的:

string[] splitted = example.Split(new[] { splitter }, StringSplitOptions.None) 
          .Select(value => value.Trim()) 
          .ToArray();