使用多个字符拆分句子?

时间:2017-08-28 05:44:22

标签: c# arrays split

我有一个输入框来输入句子,我想把它分成每个特定的字符。我已为.完成了此操作:

 var ArraySourceTexts = textbox.Text.Split(new Char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

我的问题是,如果我有多个角色怎么办?例如,如果句子包含字符:.?!,我希望将其拆分。

请分享!

1 个答案:

答案 0 :(得分:6)

string.SplitChar数组一起使用,可以在该数组中指定任意数量的字符。只需添加更多将导致拆分的字符:

char[] splitChars = new char[] { '.', '!', '?', ',' };
var ArraySourceTexts = textbox.Text.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);

输入:

this is an example. Please check, and let me know your thoughts!

输出:

[0] this is an example 
[1] Please check 
[2] and let me know your thoughts   

方法2 :如果您想拆分字符串,但保留分隔符(就像您在评论中提到的那样):

string[] arr = Regex.Split(textbox.Text, @"(?<=[.,!?])");

输入:

this is an example. Please check, and let me know your thoughts!

输出:

[0] this is an example.
[1] Please check, 
[2] and let me know your thoughts!