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. std::string question

std::string question

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 Posts 5 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.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    Consider declaration, std::string name = " ABCD" As can be seen above the string consist of many "space bar" characters in the beginning. I want to skip them and get the resultat string value. Please tell me how ????

    J T A 3 Replies Last reply
    0
    • A Anonymous

      Consider declaration, std::string name = " ABCD" As can be seen above the string consist of many "space bar" characters in the beginning. I want to skip them and get the resultat string value. Please tell me how ????

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      name.substr(name.find_first_not_of(' '));

      Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      1 Reply Last reply
      0
      • A Anonymous

        Consider declaration, std::string name = " ABCD" As can be seen above the string consist of many "space bar" characters in the beginning. I want to skip them and get the resultat string value. Please tell me how ????

        T Offline
        T Offline
        Tomasz Sowinski
        wrote on last edited by
        #3

        This would be easiest:

        std::string name = "ABCD";

        Now seriously: you should use string::find_first_not_of:

        int pos = name.find_first_not_of(' ');
        if (pos != std::string::npos)
        {
        name.erase(0, pos);
        }

        If you want to remove all spaces (not only at beginning), use this:

        name.erase(std::remove(name.begin(), name.end(), ' '), name.end());

        Tomasz Sowinski -- http://www.shooltz.com

        ** Putt knot yore thrust inn spel chequers. **

        1 Reply Last reply
        0
        • A Anonymous

          Consider declaration, std::string name = " ABCD" As can be seen above the string consist of many "space bar" characters in the beginning. I want to skip them and get the resultat string value. Please tell me how ????

          A Offline
          A Offline
          Alexandru Savescu
          wrote on last edited by
          #4

          Should you use CString you have the following methods: TrimLeft, TrimRight. Best regards, Alexandru Savescu

          J 1 Reply Last reply
          0
          • A Alexandru Savescu

            Should you use CString you have the following methods: TrimLeft, TrimRight. Best regards, Alexandru Savescu

            J Offline
            J Offline
            Joaquin M Lopez Munoz
            wrote on last edited by
            #5

            (with tremulous voice) Don't lead the guy apart from the one and true std::string! Now seriously, these functions, though not directly available, can be easily written:

            /* String trimming templates.
            * Written by Markus B. Krüger (markusk@pvv.org)
            * Found at USENET:
            * Subject: Re: trim in standard lib?
            * Newsgroups: comp.lang.c++.moderated
            * Date: 2001-07-13 19:21:08 PST
            * URL (Google): http://groups.google.com/groups?hl=en&selm=du38zhtjjze.fsf%40proto.pvv.ntnu.no
            *
            * NB: There's a typo on the original message. On trim_right(String& str, Ch ws), replace
            *
            * if ((last_nonblank = str.find_last_not_of(' ')) != String::npos)
            *
            * with
            * if ((last_nonblank = str.find_last_not_of(ws)) != String::npos)
            */
             
            #ifndef TRIMTEMPLATES_H
            #define TRIMTEMPLATES_H
             
            #define VERSION_TRIMTEMPLATES 0x00010000
             
            template<class String,class Ch>
            inline String& trim_left(String& str,Ch ws)
            {
            typename String::size_type first_nonblank;
            if ((first_nonblank=str.find_first_not_of(ws))!=String::npos)
            return str.erase(0, first_nonblank);
            else
            return str;
            }
             
            template<class String>
            inline String& trim_left(String& str)
            {
            return trim_left(str, ' ');
            }
             
            template<class String,class Ch>
            inline String& trim_right(String& str,Ch ws)
            {
            typename String::size_type last_nonblank;
            if((last_nonblank=str.find_last_not_of(ws))!= String::npos)
            return str.erase(last_nonblank+1);
            else
            return str;
            }
             
            template<class String>
            inline String& trim_right(String& str)
            {
            return trim_right(str, ' ');
            }
             
            template<class String,class Ch>
            inline String& trim(String& str,Ch ws)
            {
            return trim_left(trim_right(str, ws),ws);
            }
             
            template<class String>
            inline String& trim(String& str)
            {
            return trim_left(trim_right(str));
            }
             
            #elif VERSION_TRIMTEMPLATES!=0x00010000
            #error You have included two LISTPORTS.H with different version numbers
            #endif

            Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            A 1 Reply Last reply
            0
            • J Joaquin M Lopez Munoz

              (with tremulous voice) Don't lead the guy apart from the one and true std::string! Now seriously, these functions, though not directly available, can be easily written:

              /* String trimming templates.
              * Written by Markus B. Krüger (markusk@pvv.org)
              * Found at USENET:
              * Subject: Re: trim in standard lib?
              * Newsgroups: comp.lang.c++.moderated
              * Date: 2001-07-13 19:21:08 PST
              * URL (Google): http://groups.google.com/groups?hl=en&selm=du38zhtjjze.fsf%40proto.pvv.ntnu.no
              *
              * NB: There's a typo on the original message. On trim_right(String& str, Ch ws), replace
              *
              * if ((last_nonblank = str.find_last_not_of(' ')) != String::npos)
              *
              * with
              * if ((last_nonblank = str.find_last_not_of(ws)) != String::npos)
              */
               
              #ifndef TRIMTEMPLATES_H
              #define TRIMTEMPLATES_H
               
              #define VERSION_TRIMTEMPLATES 0x00010000
               
              template<class String,class Ch>
              inline String& trim_left(String& str,Ch ws)
              {
              typename String::size_type first_nonblank;
              if ((first_nonblank=str.find_first_not_of(ws))!=String::npos)
              return str.erase(0, first_nonblank);
              else
              return str;
              }
               
              template<class String>
              inline String& trim_left(String& str)
              {
              return trim_left(str, ' ');
              }
               
              template<class String,class Ch>
              inline String& trim_right(String& str,Ch ws)
              {
              typename String::size_type last_nonblank;
              if((last_nonblank=str.find_last_not_of(ws))!= String::npos)
              return str.erase(last_nonblank+1);
              else
              return str;
              }
               
              template<class String>
              inline String& trim_right(String& str)
              {
              return trim_right(str, ' ');
              }
               
              template<class String,class Ch>
              inline String& trim(String& str,Ch ws)
              {
              return trim_left(trim_right(str, ws),ws);
              }
               
              template<class String>
              inline String& trim(String& str)
              {
              return trim_left(trim_right(str));
              }
               
              #elif VERSION_TRIMTEMPLATES!=0x00010000
              #error You have included two LISTPORTS.H with different version numbers
              #endif

              Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

              A Offline
              A Offline
              Alexandru Savescu
              wrote on last edited by
              #6

              Joaquín M López Muñoz wrote: Don't lead the guy apart from the one and true std::string! I never meant that! I like STL just as much as you do. I was just showing some MFC code ;) Best regards, Alexandru Savescu

              B 1 Reply Last reply
              0
              • A Alexandru Savescu

                Joaquín M López Muñoz wrote: Don't lead the guy apart from the one and true std::string! I never meant that! I like STL just as much as you do. I was just showing some MFC code ;) Best regards, Alexandru Savescu

                B Offline
                B Offline
                Bill Wilson
                wrote on last edited by
                #7

                SHAME! SHAME! You should know better than to show MFC examples. People will get the mistaken impression its easier to use than the beloved STL. :-D

                A 1 Reply Last reply
                0
                • B Bill Wilson

                  SHAME! SHAME! You should know better than to show MFC examples. People will get the mistaken impression its easier to use than the beloved STL. :-D

                  A Offline
                  A Offline
                  Alexandru Savescu
                  wrote on last edited by
                  #8

                  Take it easy man! I was not giving any MFC example, I was giving a CString example. CString is a very good class it can be argued that it is better than std::string (it uses reference counting remember!) Best regards, Alexandru Savescu

                  B 1 Reply Last reply
                  0
                  • A Alexandru Savescu

                    Take it easy man! I was not giving any MFC example, I was giving a CString example. CString is a very good class it can be argued that it is better than std::string (it uses reference counting remember!) Best regards, Alexandru Savescu

                    B Offline
                    B Offline
                    Bill Wilson
                    wrote on last edited by
                    #9

                    Alexpro wrote: it is better than std::string SACRILEDGE!! Alexpro wrote: CString is a very good class BLASPHEMY!! Oh NO! STr o k e... BTW CString is part of MFC. (Microsoft Foul Code). Everyone knows was developed as part of a feindish plot to undermine portablilty with Unix/Linux.:rolleyes:

                    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