C#Regex.Split 用法



C#Regex.Split 比string.Split 丰富

例子

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "plum--pear";
      string pattern = "-";            // Split on hyphens

      string[] substrings = Regex.Split(input, pattern);
      foreach (string match in substrings)
      {
         Console.WriteLine("'{0}'", match);
      }
   }
}
// The method displays the following output:
// 'plum'
// ''
// 'pear'     

例子2

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "characters";
      string[] substrings = Regex.Split(input, "");
      Console.Write("{");
      for(int ctr = 0; ctr < substrings.Length; ctr++)
      {
         Console.Write("'{0}'", substrings[ctr]);
         if (ctr < substrings.Length - 1)
            Console.Write(", ");
      }
      Console.WriteLine("}");
   }
}
// The example produces the following output:
// {'', 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', 's', ''}

详细可以参照 https://msdn.microsoft.com/zh-cn/library/8yttk7sy.aspx

你可能感兴趣的:(C#)