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

后端 未结 3 850
[愿得一人]
[愿得一人] 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<string> 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.

    0 讨论(0)
  • 2021-01-23 16:21

    You could maybe use regex class, somthing like below, but you will need to add support for other chars other than numbers and letters.

       var chars = Regex.Matches("0015j8*(", @"((?:""[^""\\]*(?:\\.[^""\\]*)*"")|[a-z]|\d+)").Cast<Match>().Select(match => match.Value).ToArray(); 
    

    Result 0015,J,8

    0 讨论(0)
  • 2021-01-23 16:22

    I don't think that such method exist. You can follow steps as below to create your own utility method:

    1. Create a list to hold split strings
    2. Define strings with all your character types e.g.

       string numberString = "0123456789";
       string specialChars = "~!@#$%^&*(){}|\/?";
       string alphaChars = "abcde....XYZ";
      
    3. Define a variable to hold the temporary string
    4. Define a variable to note the type of chars
    5. Traverse your string, one char at a time, check the type of char by checking the presence of the char in predefined type strings.
    6. If type is new than the previous type(check the type variable value) then add the temporary string(not empty) to the list, assign the new type to type variable and assign the current char to the temp string. If otherwise, then append the char to temporary string.
    7. In the end of traversal, add the temporary string(not empty) to the list
    8. Now your list contains the split strings.
    9. Convert the list to an string array and you are done.
    0 讨论(0)
提交回复
热议问题