Most reliable split character

后端 未结 11 1992
忘掉有多难
忘掉有多难 2021-01-31 01:49

Update

If you were forced to use a single char on a split method, which char would be the most reliable?

Definition of reliable: a split charact

相关标签:
11条回答
  • 2021-01-31 02:29

    I usually prefer a '|' symbol as the split character. If you are not sure of what user enters in the text then you can restrict the user from entering some special characters and you can choose from those characters, the split character.

    0 讨论(0)
  • 2021-01-31 02:33

    It depends very much on the context in which it's used. If you're talking about a very general delimiting character then I don't think there is a one-size-fits-all answer.

    I find that the ASCII null character '\0' is often a good candidate, or you can go with nitzmahone's idea and use more than one character, then it can be as crazy as you want.

    Alternatively, you can parse the input and escape any instances of your delimiting character.

    0 讨论(0)
  • 2021-01-31 02:36

    Aside from 0x0, which may not be available (because of null-terminated strings, for example), the ASCII control characters between 0x1 and 0x1f are good candidates. The ASCII characters 0x1c-0x1f are even designed for such a thing and have the names File Separator, Group Separator, Record Separator, Unit Separator. However, they are forbidden in transport formats such as XML.

    In that case, the characters from the unicode private use code points may be used.

    One last option would be to use an escaping strategy, so that the separation character can be entered somehow anyway. However, this complicates the task quite a lot and you cannot use String.Split anymore.

    0 讨论(0)
  • 2021-01-31 02:41

    We currently use

    public const char Separator = ((char)007);
    

    I think this is the beep sound, if i am not mistaken.

    0 讨论(0)
  • 2021-01-31 02:41

    It depends what you're splitting.

    In most cases it's best to use split chars that are fairly commonly used, for instance

    value, value, value

    value|value|value

    key=value;key=value;

    key:value;key:value;

    You can use quoted identifiers nicely with commas:

    "value", "value", "value with , inside", "value"

    I tend to use , first, then |, then if I can't use either of them I use the section-break char §

    Note that you can type any ASCII char with ALT+number (on the numeric keypad only), so § is ALT+21

    0 讨论(0)
  • 2021-01-31 02:41

    There are overloads of String.Split that take string separators...

    0 讨论(0)
提交回复
热议问题