none
How to split string against multiple string patterns RRS feed

  • 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

    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
    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

    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
    Tuesday, June 12, 2018 12:13 PM