Answered by:
How to split string against multiple string patterns

Question
-
Hello All,
I am facing problem in splitting string against multiple string patterns. Please help me on this.
eg : my string is
string value = "cat[__]dog[__]animal[__]person(__)abc[__]dog1[__]animal1(__)person2[***]iobdb[***]blabla";
I want to split this string into string array against multiple string patterns like [__], (__), [***]
How can i achieve this. please help me...
Thanks
Chandu
Tuesday, June 12, 2018 10:45 AM
Answers
-
Try following code
string value = "cat[__]dog[__]animal[__]person(__)abc[__]dog1[__]animal1(__)person2[***]iobdb[***]blabla"; string[] separators = { "[__]", "(__)", "[***]" }; var items = value.Split(separators, StringSplitOptions.None);
Gaurav Khanna | Microsoft MVP | Microsoft Community Contributor
- Proposed as answer by Dolen Zhang Wednesday, June 13, 2018 8:15 AM
- Marked as answer by How to remove xml namespace prefixes Wednesday, June 13, 2018 11:29 AM
Tuesday, June 12, 2018 12:12 PM -
You can use the '|' as an 'or' operator, but don't forget to escape special characters:
string[] arr = Regex.Split(value, @"\[__\]|\(__\)|\[\*\*\*]");
wizend- Edited by Wizend Tuesday, June 12, 2018 12:14 PM
- Proposed as answer by Dolen Zhang Wednesday, June 13, 2018 8:15 AM
- Marked as answer by How to remove xml namespace prefixes Wednesday, June 13, 2018 11:29 AM
Tuesday, June 12, 2018 12:13 PM
All replies
-
Try following code
string value = "cat[__]dog[__]animal[__]person(__)abc[__]dog1[__]animal1(__)person2[***]iobdb[***]blabla"; string[] separators = { "[__]", "(__)", "[***]" }; var items = value.Split(separators, StringSplitOptions.None);
Gaurav Khanna | Microsoft MVP | Microsoft Community Contributor
- Proposed as answer by Dolen Zhang Wednesday, June 13, 2018 8:15 AM
- Marked as answer by How to remove xml namespace prefixes Wednesday, June 13, 2018 11:29 AM
Tuesday, June 12, 2018 12:12 PM -
You can use the '|' as an 'or' operator, but don't forget to escape special characters:
string[] arr = Regex.Split(value, @"\[__\]|\(__\)|\[\*\*\*]");
wizend- Edited by Wizend Tuesday, June 12, 2018 12:14 PM
- Proposed as answer by Dolen Zhang Wednesday, June 13, 2018 8:15 AM
- Marked as answer by How to remove xml namespace prefixes Wednesday, June 13, 2018 11:29 AM
Tuesday, June 12, 2018 12:13 PM