【问题标题】:split string with more than one Char in C#在 C# 中拆分具有多个字符的字符串
【发布时间】:2010-10-07 09:51:19
【问题描述】:

我想用“ER”分隔符分割 String = “Asaf_ER_Army”。 String 的 Split 功能不允许将字符串拆分多个字符。

如何用“多个字符”分隔符分割字符串?

【问题讨论】:

    标签: c# string split


    【解决方案1】:

    确实如此。阅读here

    string source = "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]";
    string[] stringSeparators = new string[] {"[stop]"};
    
    // Split a string delimited by another string and return all elements.
    string[] result = source.Split(stringSeparators, StringSplitOptions.None);
    

    编辑: 或者,您可以有一些更复杂的选择(RegEx)。在这里,http://dotnetperls.com/string-split

    【讨论】:

      【解决方案2】:

      String.Split 做你想做的事。使用接受字符串数组的重载。

      例子:

      string[] result = "Asaf_ER_Army".Split(
          new string[] {"ER"},
          StringSplitOptions.None);
      

      结果:

      阿萨夫_ _军队

      【讨论】:

        【解决方案3】:

        String.Split 的重载将字符串数组作为分隔符:http://msdn.microsoft.com/en-gb/library/1bwe3zdy%28v=VS.80%29.aspx

        除非你使用框架

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-23
          • 2012-05-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多