【问题标题】:substring at first alphanumeric char第一个字母数字字符的子字符串
【发布时间】:2011-12-07 14:59:43
【问题描述】:

我正在寻找一种在第一个字母数字字符之后将字符串分成两部分的快速方法。

我知道我可以在字符串中创建 foreach 字符并检查它是数字还是字母数字,但我不知道它是否很快。

例如我有一个字符串“25I10”

我需要将其拆分为 25 和 I10(我不能将其子串为 0,2,因为第一个数字可以更小或更大。我必须在第一个 AlphaNumeric 字符上拆分它,因为这是命令开始的地方。

它需要很快,因为我在 telnet 上收到了很多命令,我不想放慢速度。

是的,我知道我可以将字符串发送到另一个线程然后拆分它,但我不想为这样的事情创建很多线程。

有人知道最好的选择吗?

【问题讨论】:

  • 首先,在内部必须有一个遍历字符串的循环来查看它需要在哪里拆分。所以性能应该不是问题(除非你让它真的很奇怪)。
  • 当我写信给@Bengie 时,我忘记了我可以在从 telnet 读取字节时检查它,所以我不必在获得整条线路后进行另一个循环。它现在就像一个魅力;)感谢提示
  • 您要求“在第一个字母数字字符之后将字符串拆分为两个”,因此对于您的示例字符串“25|10”将是“2”和“5|10”,而不是您所显示的?你的意思是“字母”而不是“字母数字”?

标签: c# substring


【解决方案1】:
int pos = Regex.Match("123abc456", "[a-z]").Index;

你也可以测试是否匹配:

Match m = Regex.Match("123abc456", "[a-z]");
int pos = -1;
if (m.Success) {
    pos = m.Index;
}

模式\p{L} 而不是[a-z] 也将匹配来自非英语语言的重音字母。

你也可以直接用Regex进行拆分:

string[] parts = Regex.Split("123X456", @"\p{L}");

【讨论】:

    【解决方案2】:

    这是一个使用正则表达式的可编译代码:

    using System;
    using System.Text.RegularExpressions;
    
    public static class Splitter
    {
      private static Regex regex;
    
      static Splitter()
      {
        string separator = "[a-zA-Z]";
        string notSeparator = "[^a-zA-Z]";
        string pattern = "^(?<left>" + notSeparator + "+)(?<right>" + separator + ".*)$";
        regex = new Regex(pattern, RegexOptions.Compiled | RegexOptions.ExplicitCapture);
      }
    
      public static bool Split(string input, out string left, out string right)
      {
        var match = regex.Match(input);
        if (match.Success && match.Groups["left"].Captures.Count == 1 && match.Groups["right"].Captures.Count == 1)
        {
          left = match.Groups["left"].Captures[0].Value;
          right = match.Groups["right"].Captures[0].Value;
          return true;
        }
        else
        {
          left = null;
          right = null;
          return false;
        }
      }
    }
    
    public static class Program
    {
      public static void Test(string input)
      {
        string left, right;
        if (Splitter.Split(input, out left, out right))
          Console.WriteLine("\"" + input + "\" -> \"" + left + "\" + \"" + right + "\"");
        else
          Console.WriteLine("The string \"" + input + "\" could not be split");
      }
    
      public static void Main()
      {
        Test("2510");
        Test("2510I");
        Test("I2510");
        Test("25I10");
      }
    }
    

    输出:

    The string "2510" could not be split
    "2510I" -> "2510" + "I"
    The string "I2510" could not be split
    "25I10" -> "25" + "I10"
    

    【讨论】:

      【解决方案3】:
      string str = "10|25";
      string[] strArray = System.Text.RegularExpressions.Regex.Split(str, @"(\D.*$)");
      

      它给你一个长度为 3 的数组,第一个和第二个元素是你想要的字符串,最后一个元素总是“”。

      【讨论】:

        猜你喜欢
        • 2020-02-02
        • 1970-01-01
        • 1970-01-01
        • 2013-09-24
        • 1970-01-01
        • 1970-01-01
        • 2013-10-02
        • 2020-07-02
        • 1970-01-01
        相关资源
        最近更新 更多