使用多个字符拆分句子?

Ras*_*rya 2 c# arrays split

我有一个输入框来输入句子,我想把它分成每个特定的字符.我这样做是为了.:

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

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

请分享!

Kob*_*uek 6

使用string.SplitChar数组,你可以为你想要的阵列中指定尽可能多的字符.只需添加更多将导致拆分的字符:

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

输入:

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   
Run Code Online (Sandbox Code Playgroud)

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

string[] arr = Regex.Split(textbox.Text, @"(?<=[.,!?])");
Run Code Online (Sandbox Code Playgroud)

输入:

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!   
Run Code Online (Sandbox Code Playgroud)