2017-08-28 24 views
2

我有一个输入框来输入句子,我想分割它在每个特定的字符。我这样做对.使用多个字符分割一个句子?

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

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

请分享!

+2

不知道是什么问题...你是已经使用数组重载 - 所以指定更多的项目:'new Char [] {'。' ,'?','!'}' –

+0

你的问题不清楚。你想达到什么目的?你能提供例子和期望的输出吗? –

+0

如果我有这样的例子: 输入文本:这是一个例子。请检查,并让我知道你的想法! 所以我想从文本框中分割句子,用“。”表示。和“,”。 所以我想输出有3个数组。 ' 1.这是一个例子。 2.请检查, 3.让我知道你的想法! –

回答

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:如果要拆分字符串但是ke EP的分隔符(如你在评论中提到的):

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!