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. General Programming
  3. C#
  4. Question about split

Question about split

Scheduled Pinned Locked Moved C#
question
14 Posts 4 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.
  • S Offline
    S Offline
    S Douglas
    wrote on last edited by
    #1

    Trying to use the split function, but one of the fields I am trying to load has "Last Name, First Name" (with double quotes around it. Is it posible to set an optionaly enclosed by parameter in the split function?


    Common sense is admitting there is cause and effect and that you can exert some control over what you understand.

    P 1 Reply Last reply
    0
    • S S Douglas

      Trying to use the split function, but one of the fields I am trying to load has "Last Name, First Name" (with double quotes around it. Is it posible to set an optionaly enclosed by parameter in the split function?


      Common sense is admitting there is cause and effect and that you can exert some control over what you understand.

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      No, but see Rive[^].

      S S 2 Replies Last reply
      0
      • P PIEBALDconsult

        No, but see Rive[^].

        S Offline
        S Offline
        S Douglas
        wrote on last edited by
        #3

        Thanks


        Common sense is admitting there is cause and effect and that you can exert some control over what you understand.

        1 Reply Last reply
        0
        • P PIEBALDconsult

          No, but see Rive[^].

          S Offline
          S Offline
          SledgeHammer01
          wrote on last edited by
          #4

          wouldn't it be easier to: strName.Replace("\"", "").Split(...)?

          P 1 Reply Last reply
          0
          • S SledgeHammer01

            wouldn't it be easier to: strName.Replace("\"", "").Split(...)?

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Not if he wants the name to stay whole (which I assume to be the case). That [what Sledge suggests] seems like a bad idea in general.

            S 1 Reply Last reply
            0
            • P PIEBALDconsult

              Not if he wants the name to stay whole (which I assume to be the case). That [what Sledge suggests] seems like a bad idea in general.

              S Offline
              S Offline
              S Douglas
              wrote on last edited by
              #6

              Your assumption is correct. Needed the whole name. To get it done, I changed the delimiter in the data from a comma to a tilde. It worked users are happy for the moment. (Edit) Part of the conversion process is breaking the name apart into first name last name


              Common sense is admitting there is cause and effect and that you can exert some control over what you understand.

              S 1 Reply Last reply
              0
              • S S Douglas

                Your assumption is correct. Needed the whole name. To get it done, I changed the delimiter in the data from a comma to a tilde. It worked users are happy for the moment. (Edit) Part of the conversion process is breaking the name apart into first name last name


                Common sense is admitting there is cause and effect and that you can exert some control over what you understand.

                S Offline
                S Offline
                SledgeHammer01
                wrote on last edited by
                #7

                I don't get it. You have a string as "Last Name, First Name" and you want Last Name, First Name without the quotes? Then just strip off the quotes. There is no need to use the split function. Since you want to break the name apart, my solution is actually correct. Remove the quotes, then split based on the comma.

                P A 2 Replies Last reply
                0
                • S SledgeHammer01

                  I don't get it. You have a string as "Last Name, First Name" and you want Last Name, First Name without the quotes? Then just strip off the quotes. There is no need to use the split function. Since you want to break the name apart, my solution is actually correct. Remove the quotes, then split based on the comma.

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  No, that's just one of the fields. Check out some troublesome data:

                  id,name,location,phone
                  1,"Smith, John","Anytown, USA",123-4567
                  2,William "The Refrigerator" Perry,The Gridiron,000-0000
                  3,Madonna,"Los Feliz, Los Angeles, CA",555-5555

                  Not to say that the OP has such problems with which to deal, but some do.

                  S 1 Reply Last reply
                  0
                  • S SledgeHammer01

                    I don't get it. You have a string as "Last Name, First Name" and you want Last Name, First Name without the quotes? Then just strip off the quotes. There is no need to use the split function. Since you want to break the name apart, my solution is actually correct. Remove the quotes, then split based on the comma.

                    A Offline
                    A Offline
                    April Fans
                    wrote on last edited by
                    #9

                    This is actually what I would've done too - it's seems simpler. But then there are also multiple fields involved.

                    April Comm100 - Leading Live Chat Software Provider

                    1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      No, that's just one of the fields. Check out some troublesome data:

                      id,name,location,phone
                      1,"Smith, John","Anytown, USA",123-4567
                      2,William "The Refrigerator" Perry,The Gridiron,000-0000
                      3,Madonna,"Los Feliz, Los Angeles, CA",555-5555

                      Not to say that the OP has such problems with which to deal, but some do.

                      S Offline
                      S Offline
                      SledgeHammer01
                      wrote on last edited by
                      #10

                      I don't recall seeing that in the original post. I went back and re-read it, and if that is what the OP had intended, its a very poorly worded question :). In this case, I'd: string[] parts = Split(',') foreach (string in parts) Replace("\"", ""); Personally, parts 1 and 2 need special comma handling and parts 0 and 3 don't. If I wanted to get sophisticated, I believe you can use datasets to parse something like that for you, but I'd just do what I said above... split on the commas, strip off the quotes and then process the 4 fields.

                      P 2 Replies Last reply
                      0
                      • S SledgeHammer01

                        I don't recall seeing that in the original post. I went back and re-read it, and if that is what the OP had intended, its a very poorly worded question :). In this case, I'd: string[] parts = Split(',') foreach (string in parts) Replace("\"", ""); Personally, parts 1 and 2 need special comma handling and parts 0 and 3 don't. If I wanted to get sophisticated, I believe you can use datasets to parse something like that for you, but I'd just do what I said above... split on the commas, strip off the quotes and then process the 4 fields.

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #11

                        William "The Refrigerator" Perry and Madonna say that's a bad idea. Without more knowledge of the data, the only proper way to handle this is to split on the delimiters while honoring the quotes. Replace is rarely an appropriate tool.

                        1 Reply Last reply
                        0
                        • S SledgeHammer01

                          I don't recall seeing that in the original post. I went back and re-read it, and if that is what the OP had intended, its a very poorly worded question :). In this case, I'd: string[] parts = Split(',') foreach (string in parts) Replace("\"", ""); Personally, parts 1 and 2 need special comma handling and parts 0 and 3 don't. If I wanted to get sophisticated, I believe you can use datasets to parse something like that for you, but I'd just do what I said above... split on the commas, strip off the quotes and then process the 4 fields.

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #12

                          SledgeHammer01 wrote:

                          its a very poorly worded question :)

                          Those are some of the lines I've become better at reading between. If it were that simple, the question wouldn't have been asked (by this particular asker).

                          S 2 Replies Last reply
                          0
                          • P PIEBALDconsult

                            SledgeHammer01 wrote:

                            its a very poorly worded question :)

                            Those are some of the lines I've become better at reading between. If it were that simple, the question wouldn't have been asked (by this particular asker).

                            S Offline
                            S Offline
                            SledgeHammer01
                            wrote on last edited by
                            #13

                            Oh... actually, I just re-read the OPs post. We have BOTH misinterpreted it. His issue is that: a,b,"c,d",e is getting split by comma as a,b,c,d,e instead of c,d getting split as a single item. That has nothing to do with Replace or stripping quotes. That's simply an issue of Split doing what its supposed to do. Splitting on the comma. Split is the issue here, not Replace. Assuming data is never misformed, he'd have to "escape" the commas inside of double quotes. Which is what he did. This is why you shouldn't use CSV files in general. TSV or PSV files are much less likely to run into this problem. The PROPER solution when using CSV, TSV or PSV files is to escape separators when WRITING if they occur in your string. Much like having a < or > is not valid XML.

                            1 Reply Last reply
                            0
                            • P PIEBALDconsult

                              SledgeHammer01 wrote:

                              its a very poorly worded question :)

                              Those are some of the lines I've become better at reading between. If it were that simple, the question wouldn't have been asked (by this particular asker).

                              S Offline
                              S Offline
                              SledgeHammer01
                              wrote on last edited by
                              #14

                              Oh, NM... I see his issue now.

                              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