Most reliable split character

后端 未结 11 1993
忘掉有多难
忘掉有多难 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:43

    You can safely use whatever character you like as delimiter, if you escape the string so that you know that it doesn't contain that character.

    Let's for example choose the character 'a' as delimiter. (I intentionally picked a usual character to show that any character can be used.)

    Use the character 'b' as escape code. We replace any occurrence of 'a' with 'b1' and any occurrence of 'b' with 'b2':

    private static string Escape(string s) {
       return s.Replace("b", "b2").Replace("a", "b1");
    }
    

    Now, the string doesn't contain any 'a' characters, so you can put several of those strings together:

    string msg = Escape("banana") + "a" + Escape("aardvark") + "a" + Escape("bark");
    

    The string now looks like this:

    b2b1nb1nb1ab1b1rdvb1rkab2b1rk
    

    Now you can split the string on 'a' and get the individual parts:

    b2b1nb1nb1
    b1b1rdvb1rk
    b2b1rk
    

    To decode the parts you do the replacement backwards:

    private static string Unescape(string s) {
       return s.Replace("b1", "a").Replace("b2", "b");
    }
    

    So splitting the string and unencoding the parts is done like this:

    string[] parts = msg.split('a');
    for (int i = 0; i < parts.length; i++) {
      parts[i] = Unescape(parts[i]);
    }
    

    Or using LINQ:

    string[] parts = msg.Split('a').Select(Unescape).ToArray();
    

    If you choose a less common character as delimiter, there are of course fewer occurrences that will be escaped. The point is that the method makes sure that the character is safe to use as delimiter without making any assumptions about what characters exists in the data that you want to put in the string.

提交回复
热议问题