我有下一个字符串
string example="This is a string and i want to split by [this], it's posible?";
是否有任何方法允许我在找到某个单词或字符串时拆分字符串,例如“[this]”或“split”
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?";
最好的方法是什么?
答案 0 :(得分: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();