is there a splitByCharacterType method in c# like there is in Java?

后端 未结 3 886
[愿得一人]
[愿得一人] 2021-01-23 16:05

In Java there is a method splitByCharacterType that takes a string, for example 0015j8*(, and split it into \"0015\",\"j\",\"8\",\"*\",\"(\". Is there

3条回答
  •  死守一世寂寞
    2021-01-23 16:15

    public static IEnumerable SplitByCharacterType(string input)
    {
        if (String.IsNullOrEmpty(input))
            throw new ArgumentNullException(nameof(input));
    
        StringBuilder segment = new StringBuilder();
        segment.Append(input[0]);
        var current = Char.GetUnicodeCategory(input[0]);
    
        for (int i = 1; i < input.Length; i++)
        {
            var next = Char.GetUnicodeCategory(input[i]);
            if (next == current)
            {
                segment.Append(input[i]);
            }
            else
            {
                yield return segment.ToString();
                segment.Clear();
                segment.Append(input[i]);
                current = next;
            }
        }
        yield return segment.ToString();
    }
    

    Usage as follows:

    string[] split = SplitByCharacterType("0015j8*(").ToArray();
    

    And the result is "0015","j","8","*","("

    I recommend you implement as an extension method.

提交回复
热议问题