30秒学会 C# 片段 · 2018年1月19日

30秒学会 C# 片段 – SplitStringBy

Splits a string into an array of strings using a multicharacter (string) separator.

Use string.Split() with the given separator to split the string into an array of strings.

代码实现

using System.Collections.Generic;

public static partial class _30s 
{
  public static string[] SplitStringBy(string s, string separator) 
  {
    return s.Split(new [] {separator}, StringSplitOptions.None);
  }
}

使用样例

string s = "Apples--oranges--pears";

_30s.SplitStringBy(s,"--"); // {Apples, oranges, pears}