string.split

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • guimel
    New Member
    • Oct 2009
    • 7

    string.split

    According to MSDN, All of the String.Split overloads take an array of char or an array of string as first parameter. However,

    string str = "Hello World";
    string[] split = str.Split(' ');

    works as well. Is there some sort of scalar promotion to array in C# or is there an overload missing in the description of string.split ?

    Cheers,

    Guillaume
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Oh , you were so close to the answer too since you were already on MSDN.

    According to MSDN a string is a char[] terminating with a char(0) or "\0"
    Originally posted by MSDN
    A String object is a sequential collection of System.Char objects that represent a string. The value of the String object is the content of the sequential collection, and that value is immutable.
    So when you send string.split what you see as a string, you are already sending it a byte array

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      I think the OP was getting at being able to send a single ' ' character (as opposed to an array of characters). In regular .NET yes, it treats a single character (it might just be a hidden overload) as an array of them.
      In the compact framework it does not

      Comment

      • guimel
        New Member
        • Oct 2009
        • 7

        #4
        Indeed, I'm only passing a single char not a single string.

        Furthermore, even if a string is a collection of char, I don't believe it is the same as an array of char:
        Code:
        string str="Hello World";
        char[] strconv1 = str; //error CS0029: Cannot implicitly convert type 'string' to 'char[]'
        char[] strconv2 = (char[]) str; //error CS0030: Cannot convert type 'string' to 'char[]'
        If it's a hidden overload, is it safe to use ? I guess not, since it's undocumented.

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          I guess I'm missing the point.
          You would split a string based on a character such as a comma or a tab.
          I would expect to send .Split() a single character to use as the delimiter.
          A single char is little more than an array with one element.

          We see the framework make these kinds of 'intuitive' adjustments all the time.
          Doing math with floats and ints for example. Floats get turned into ints under certain math operations despite the lack of explicit casting to an int.

          float = int/int will give you an int not a float unless you
          float = (float)int / (float)int

          Comment

          • guimel
            New Member
            • Oct 2009
            • 7

            #6
            Originally posted by tlhintoq
            I guess I'm missing the point.
            You would split a string based on a character such as a comma or a tab.
            I would expect to send .Split() a single character to use as the delimiter.
            So would I, but the documentation doesn't say so.

            A single char is little more than an array with one element.
            I doubt a function that expect an array would accept a scalar. The first one needs to know the size of the array somehow.

            We see the framework make these kinds of 'intuitive' adjustments all the time.
            The problem is that 'intuitive', if not documented, may not work tomorrow or under some special conditions. You don't know, because it's not documented.

            Floats get turned into ints under certain math operations despite the lack of explicit casting to an int.
            Do they ?

            float = int/int will give you an int not a float unless you
            float = (float)int / (float)int
            That's implicit conversion from int to float. That's documented behaviour.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              To go from string -> char[]
              Code:
              string s=" ";
              s.ToCharArray();
              And I am saying the .Split() function might have hidden overloads that accept a char instead of char[].

              Yes a single char is similar to a char array of size one, like I said, the compact .NET doesn't allow that conversion, I have to use the above code.

              Comment

              • guimel
                New Member
                • Oct 2009
                • 7

                #8
                Originally posted by Plater
                Yes a single char is similar to a char array of size one,
                It may be similar but it's not the same. You can't substitute a single char for a char array (or a string). One has a size information attached to it, the other not.

                And I am saying the .Split() function might have hidden overloads that accept a char instead of char[].
                What's the point of a public undocumented overload ? You don't know what it may throw, what pre and post condition it assumes and if it'll be working in the next iteration of the framework.

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  Is this really a big issue for you? That you can use a single char? If you don't like it, use a real character array and don't worry about it?

                  Comment

                  Working...