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 / C++ / MFC
  4. string parsing

string parsing

Scheduled Pinned Locked Moved C / C++ / MFC
jsonhelp
12 Posts 6 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.
  • P pix_programmer

    Use

    std::string::find_first_of()

    To find the position of #. Then use

    std::string::substring()

    to extract the string you required. Code might look like this:

    std::string test = login#user#1 username password;
    size_t pos = test.find_first_of("#");
    std::string requiredstr = test.substr(pos+1);

    R Offline
    R Offline
    robin700
    wrote on last edited by
    #3

    is there any way to do it using strchr

    P 1 Reply Last reply
    0
    • R robin700

      is there any way to do it using strchr

      P Offline
      P Offline
      pix_programmer
      wrote on last edited by
      #4

      Yes. strchr() also returns a pointer to a first occurence of a character in a string. For this you've to assign your input string to a

      char *

      variable. The following link might help: http://www.cplusplus.com/reference/clibrary/cstring/strchr/[^]

      R 1 Reply Last reply
      0
      • P pix_programmer

        Yes. strchr() also returns a pointer to a first occurence of a character in a string. For this you've to assign your input string to a

        char *

        variable. The following link might help: http://www.cplusplus.com/reference/clibrary/cstring/strchr/[^]

        R Offline
        R Offline
        robin700
        wrote on last edited by
        #5

        hey i am programming in C...can u give a sample code of how to do it using strchr(), strtok or any other function in C

        _ P 2 Replies Last reply
        0
        • R robin700

          hey i am programming in C...can u give a sample code of how to do it using strchr(), strtok or any other function in C

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #6

          http://msdn.microsoft.com/en-us/library/2c8d19sb(v=vs.71).aspx[^]

          «_Superman_»  _I love work. It gives me something to do between weekends.

          _Microsoft MVP (Visual C++)

          Polymorphism in C

          R 1 Reply Last reply
          0
          • R robin700

            hey i am programming in C...can u give a sample code of how to do it using strchr(), strtok or any other function in C

            P Offline
            P Offline
            pix_programmer
            wrote on last edited by
            #7

            I've provided you a link with source code. Use the formula in the printf statement inside the while loop. char str[] = "Your string goes here"; char * pch; printf ("Looking for the '#' character in \"%s\"...\n",str); pch=strchr(str,'s'); while (pch!=NULL) { printf ("found at %d\n",pch-str+1); pch=strchr(pch+1,'s'); } pch is the required string.

            1 Reply Last reply
            0
            • _ _Superman_

              http://msdn.microsoft.com/en-us/library/2c8d19sb(v=vs.71).aspx[^]

              «_Superman_»  _I love work. It gives me something to do between weekends.

              _Microsoft MVP (Visual C++)

              Polymorphism in C

              R Offline
              R Offline
              robin700
              wrote on last edited by
              #8

              yeah i know this but this does not work for the string Login#User#1 username password if want to trim Login# and extract User#1 username password

              _ 1 Reply Last reply
              0
              • R robin700

                yeah i know this but this does not work for the string Login#User#1 username password if want to trim Login# and extract User#1 username password

                _ Offline
                _ Offline
                _Superman_
                wrote on last edited by
                #9

                You should be able to figure out what changes to the program needs to be made for it to work with your string.

                «_Superman_»  _I love work. It gives me something to do between weekends.

                _Microsoft MVP (Visual C++)

                Polymorphism in C

                1 Reply Last reply
                0
                • R robin700

                  i have a string login#user#1 username password i want to trim login# and extract user#1 username password i am unable to sole it using strtok please help

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #10

                  Use strchr() to find the first # character, and then you can use strtok() to split into the three tokens delimited by spaces.

                  Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                  1 Reply Last reply
                  0
                  • R robin700

                    i have a string login#user#1 username password i want to trim login# and extract user#1 username password i am unable to sole it using strtok please help

                    S Offline
                    S Offline
                    Stefan_Lang
                    wrote on last edited by
                    #11

                    After so many questions regarding strings, parsing and related stuff, don't you think it's about time to read some actual books or articles to learn all this? Or do you intend to keep asking for the rest of your life? Seriously, the time you keep spending on low level programming questions like this would have been better spent by reading up and learning something by yourself. You are doing yourself and everyone else a disservice by asking questions without ever even trying to look something up and really understand the things you are dealing with. Regarding this particular question, simply reading up on strtok would solve it, easily.

                    1 Reply Last reply
                    0
                    • R robin700

                      i have a string login#user#1 username password i want to trim login# and extract user#1 username password i am unable to sole it using strtok please help

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #12

                      Have you tried something like:

                      char *pszSource = "login#user#1 username password";
                      char *pszPos = strstr(pszSource, "user#1");
                      // at this point, you could use pszPos with strcpy()
                      int nLen = pszSource - pszPos;

                      "One man's wage rise is another man's price increase." - Harold Wilson

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                      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