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. String split?

String split?

Scheduled Pinned Locked Moved C#
helpquestion
7 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
    SRKSHOME
    wrote on last edited by
    #1

    Hi, I have following string. "sdate=2,edate=3,,frq=price(sdate=0,,),curn=usd" I would like to split the above string as below. string[0]="sdate=2" string[1]="edate=3" string[2]="frq=price(sdate=0,,)" string[3]="curn=usd" I tried to split based on ',', but I didn't get as above? Please help.

    A P _ 3 Replies Last reply
    0
    • S SRKSHOME

      Hi, I have following string. "sdate=2,edate=3,,frq=price(sdate=0,,),curn=usd" I would like to split the above string as below. string[0]="sdate=2" string[1]="edate=3" string[2]="frq=price(sdate=0,,)" string[3]="curn=usd" I tried to split based on ',', but I didn't get as above? Please help.

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      SRKSHOME wrote:

      edate=3,,frq

      The thing is you have two ,'s here. Based on this string[2] will be "".

      The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick Visit the Hindi forum here.

      modified on Thursday, October 21, 2010 2:29 PM

      S 1 Reply Last reply
      0
      • S SRKSHOME

        Hi, I have following string. "sdate=2,edate=3,,frq=price(sdate=0,,),curn=usd" I would like to split the above string as below. string[0]="sdate=2" string[1]="edate=3" string[2]="frq=price(sdate=0,,)" string[3]="curn=usd" I tried to split based on ',', but I didn't get as above? Please help.

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

        Look into Regular Expressions. Also see what I did here[^].

        1 Reply Last reply
        0
        • A Abhinav S

          SRKSHOME wrote:

          edate=3,,frq

          The thing is you have two ,'s here. Based on this string[2] will be "".

          The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick Visit the Hindi forum here.

          modified on Thursday, October 21, 2010 2:29 PM

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

          no..string[1] would be edate=3 and string[2] would be ""

          A 1 Reply Last reply
          0
          • S SRKSHOME

            Hi, I have following string. "sdate=2,edate=3,,frq=price(sdate=0,,),curn=usd" I would like to split the above string as below. string[0]="sdate=2" string[1]="edate=3" string[2]="frq=price(sdate=0,,)" string[3]="curn=usd" I tried to split based on ',', but I didn't get as above? Please help.

            _ Offline
            _ Offline
            _Erik_
            wrote on last edited by
            #5

            A simple Split will work for every ',' character into the original string, and it seems that it should ignore the ones which are between parenthesis. You can use regular expressions to achieve this. First of all, replace every ',' character which is between parenthesis with another character easy to replace back when split is finished ('|' could be a good choice). Then make a normal split, and then replace back. This code would work for the sample given:

            string originalString = "sdate=2,edate=3,,frq=price(sdate=0,,),curn=usd";
            Regex r=new Regex("\\(.+\\)");

            string modified = r.Replace(originalString, m =>
            {
            return m.Value.Replace(',', '|');
            });

            string[] vector = modified.Split(',');

            for (int i = 0; i < vector.Length; i++)
            vector[i] = vector[i].Replace('|', ',');

            Though vector[3] would be an empty string due to the two consecutive commas after the edate=3. If you want to ignore several consecutive commas into the string, you should make this before the split and after the replacement made by the Regex object:

            while (modified.Contains(",,"))
            modified = modified.Replace(",,", ",");

            Hope this helps. See you

            S 1 Reply Last reply
            0
            • S SRKSHOME

              no..string[1] would be edate=3 and string[2] would be ""

              A Offline
              A Offline
              Abhinav S
              wrote on last edited by
              #6

              Missed that - thanks.

              The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick Visit the Hindi forum here.

              1 Reply Last reply
              0
              • _ _Erik_

                A simple Split will work for every ',' character into the original string, and it seems that it should ignore the ones which are between parenthesis. You can use regular expressions to achieve this. First of all, replace every ',' character which is between parenthesis with another character easy to replace back when split is finished ('|' could be a good choice). Then make a normal split, and then replace back. This code would work for the sample given:

                string originalString = "sdate=2,edate=3,,frq=price(sdate=0,,),curn=usd";
                Regex r=new Regex("\\(.+\\)");

                string modified = r.Replace(originalString, m =>
                {
                return m.Value.Replace(',', '|');
                });

                string[] vector = modified.Split(',');

                for (int i = 0; i < vector.Length; i++)
                vector[i] = vector[i].Replace('|', ',');

                Though vector[3] would be an empty string due to the two consecutive commas after the edate=3. If you want to ignore several consecutive commas into the string, you should make this before the split and after the replacement made by the Regex object:

                while (modified.Contains(",,"))
                modified = modified.Replace(",,", ",");

                Hope this helps. See you

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

                Hi Erik, This is realy helpful...Thank you very much. Thanks.

                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