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

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

提交回复
热议问题