如何用单词分割字符串
本文关键字:字符串 分割 何用单 | 更新日期: 2023-09-27 18:20:00
我有下一个字符串
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?";
最好的方法是什么?
您可以使用string.Split
:
string[] splitted = example.Split(new[] { splitter }, StringSplitOptions.None);
如果你想去除分割部分中的白色字符(尤其是在输入示例中的分割器周围):
string[] splitted = example.Split(new[] { splitter }, StringSplitOptions.None)
.Select(value => value.Trim())
.ToArray();