Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Is this still just me : c# string split and join

Is this still just me : c# string split and join

Scheduled Pinned Locked Moved The Lounge
csharpdata-structures
14 Posts 10 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M maze3

    Just written some code which needed converting an array to a string and back, and was like WTF. to split a string into an array

    var joined_str = String.Join(",", my_array);

    cool. To split a string into array:

    var my_array = joined_str.Split(',');

    Split only really wants a char, and Join only wants a string. Why can't this two things not get along. Maybe just me and some outof date c#.net :(

    M Offline
    M Offline
    Marc Clifton
    wrote on last edited by
    #4

    maze3 wrote:

    Split only really wants a char, and Join only wants a string.

    Usually splitting a string by just a char makes sense, however you should look at the method signature, it's actually not a char:

    public String[] Split(params char[] separator);

    (One of several array overloads) Joining by a string makes tons of sense. For example, joining by CRLF.

    Latest Article - Azure Function - Compute Pi Stress Test Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

    1 Reply Last reply
    0
    • M maze3

      Just written some code which needed converting an array to a string and back, and was like WTF. to split a string into an array

      var joined_str = String.Join(",", my_array);

      cool. To split a string into array:

      var my_array = joined_str.Split(',');

      Split only really wants a char, and Join only wants a string. Why can't this two things not get along. Maybe just me and some outof date c#.net :(

      C Offline
      C Offline
      CodeWraith
      wrote on last edited by
      #5

      maze3 wrote:

      var joined_str

      maze3 wrote:

      var my_array

      You don't mention why those two don't work well together, but could that overused three letter keyword be what obscures the actual problem?

      I have lived with several Zen masters - all of them were cats. His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.

      1 Reply Last reply
      0
      • M maze3

        Just written some code which needed converting an array to a string and back, and was like WTF. to split a string into an array

        var joined_str = String.Join(",", my_array);

        cool. To split a string into array:

        var my_array = joined_str.Split(',');

        Split only really wants a char, and Join only wants a string. Why can't this two things not get along. Maybe just me and some outof date c#.net :(

        R Offline
        R Offline
        realJSOP
        wrote on last edited by
        #6

        BTW, I have a persistent string parser article I wrote in 2008. It might give to a starting point for rolling your own code. Keep in mind that I wrote this when I was just starting out in .Net, so refactoring it might be a good call. Persistent String Parser[^]

        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
        -----
        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
        -----
        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

        1 Reply Last reply
        0
        • M maze3

          Just written some code which needed converting an array to a string and back, and was like WTF. to split a string into an array

          var joined_str = String.Join(",", my_array);

          cool. To split a string into array:

          var my_array = joined_str.Split(',');

          Split only really wants a char, and Join only wants a string. Why can't this two things not get along. Maybe just me and some outof date c#.net :(

          M Offline
          M Offline
          musefan
          wrote on last edited by
          #7

          I am struggling to understand why you need to convert array to string, and then back to array?

          OriginalGriffO M M 3 Replies Last reply
          0
          • M musefan

            I am struggling to understand why you need to convert array to string, and then back to array?

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #8

            To be sure, to be sure... ;)

            Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            • M musefan

              I am struggling to understand why you need to convert array to string, and then back to array?

              M Offline
              M Offline
              maze3
              wrote on last edited by
              #9

              Hahaha, I should have pointed out me simply ranting. 10 second model/viewmodel with conversion of an old property. "oh, but why not use auto models framework XX YY, that does this with an attribute tag MM!" Life.

              1 Reply Last reply
              0
              • M musefan

                I am struggling to understand why you need to convert array to string, and then back to array?

                M Offline
                M Offline
                Marc Clifton
                wrote on last edited by
                #10

                musefan wrote:

                I am struggling to understand why you need to convert array to string, and then back to array?

                It's talking to a COBOL service. ;P

                Latest Article - Azure Function - Compute Pi Stress Test Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                1 Reply Last reply
                0
                • R realJSOP

                  You can write an extension method for split that calls join to hide the nastiness, and always call split from then on.

                  ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                  -----
                  You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                  -----
                  When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                  C Offline
                  C Offline
                  Chris Maunder
                  wrote on last edited by
                  #11

                  I was just about to write the exact same message. :thumbsup:

                  cheers Chris Maunder

                  1 Reply Last reply
                  0
                  • M maze3

                    Just written some code which needed converting an array to a string and back, and was like WTF. to split a string into an array

                    var joined_str = String.Join(",", my_array);

                    cool. To split a string into array:

                    var my_array = joined_str.Split(',');

                    Split only really wants a char, and Join only wants a string. Why can't this two things not get along. Maybe just me and some outof date c#.net :(

                    R Offline
                    R Offline
                    Ravi Bhavnani
                    wrote on last edited by
                    #12

                    maze3 wrote:

                    Split only really wants a char,

                    Not sure what you mean by "only really". :)   It also accepts an array of char separators.  But I wish it also had an overload that accepted a string that was interpreted as a set of chars.  It's much easier to type. /ravi

                    My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                    1 Reply Last reply
                    0
                    • M maze3

                      Just written some code which needed converting an array to a string and back, and was like WTF. to split a string into an array

                      var joined_str = String.Join(",", my_array);

                      cool. To split a string into array:

                      var my_array = joined_str.Split(',');

                      Split only really wants a char, and Join only wants a string. Why can't this two things not get along. Maybe just me and some outof date c#.net :(

                      R Offline
                      R Offline
                      Rob Philpott
                      wrote on last edited by
                      #13

                      Am I missing something? There's an overload, albeit an ugly one:

                      string s = "Rob and Bob and Clob";
                      var z = s.Split(new[] {" and "}, StringSplitOptions.None);

                      Regards, Rob Philpott.

                      1 Reply Last reply
                      0
                      • R realJSOP

                        You can write an extension method for split that calls join to hide the nastiness, and always call split from then on.

                        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                        -----
                        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                        -----
                        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                        R Offline
                        R Offline
                        realJSOP
                        wrote on last edited by
                        #14

                        I had never used Join before, and after looking it up, it doesn't do the same thing as split, so my original response is crap. Join concatenates strings together, while split breaks a string up based on a delimiter character.

                        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                        -----
                        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                        -----
                        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups