Bhushan Muttha
Bhushan Muttha

Reputation: 430

Split a Content by list of strings

I am trying to apply the split words on and email content .It would be helpful let me know if something is missing or any workaround

Have tried below but it is not excepted result

List<string> emailSplitterStrings = new List<string>();
            emailSplitterStrings.Add("as");
            emailSplitterStrings.Add(".");
            string content1 = "as Hey Cassie! as u know i am fine.reply soon";            
            List<string> split = content1.Split(emailSplitterStrings.ToArray(), StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList();

Above code gives results : "Hey C","sie!","u know i am fine","reply soon"

Expected result : "Hey Cassie!","u know i am fine","reply soon"

Upvotes: 1

Views: 73

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522094

One approach would be to use a regex split, with an alternation pattern consisting of the splitter stings from your original script:

List<string> emailSplitterStrings = new List<string>();
emailSplitterStrings.Add("as");
emailSplitterStrings.Add(".");
string content1 = "as Hey Cassie! as u know i am fine.reply soon";
string regex = @"\b(?:" + string.Join("|", emailSplitterStrings.Select(Regex.Escape).ToArray()) + @")\b";
List<string> split = Regex.Split(content1, regex).Select(x => x.Trim()).ToList();
split.ForEach(Console.WriteLine);

This prints:

Hey Cassie!
u know i am fine
reply soon

Note that the regex pattern we are actually splitting on here is:

\b(?:as|\.)\b

We place word boundaries around the alternation, in parentheses, to make sure that as only matches the standalone word, and does not match things like the as in Cassie.

Upvotes: 2

Related Questions