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. trim heading and trailing space

trim heading and trailing space

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

    Hello everyone, I think in built-in C/C++ functions, there is no direct function call to trim heading and trailing space characters, right? So, we have to implement it manually by iterating the string to check character one by one. Support not using MFC. thanks in advance, George

    C H 2 Replies Last reply
    0
    • G George_George

      Hello everyone, I think in built-in C/C++ functions, there is no direct function call to trim heading and trailing space characters, right? So, we have to implement it manually by iterating the string to check character one by one. Support not using MFC. thanks in advance, George

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      http://www.codeproject.com/vcpp/stl/stdstringtrim.asp[^]

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      G 1 Reply Last reply
      0
      • G George_George

        Hello everyone, I think in built-in C/C++ functions, there is no direct function call to trim heading and trailing space characters, right? So, we have to implement it manually by iterating the string to check character one by one. Support not using MFC. thanks in advance, George

        H Offline
        H Offline
        henk21cm
        wrote on last edited by
        #3

        G'day George, dBaseIII had a popular function for this purpose, alltrim. In the mid nineties i needed that function in C and wrote the following implementation, plain C code. char *alltrim( char *t) { unsigned char *t_, *tc_; int i=0, l; l = strlen(t); tc_ = (unsigned char*) t; /* Remove all spaces before any usefull characters */ while (*(tc_+i) <= ' ' && i < l) i++; if (i==l) /* Everything is white space */ { *t = (char) 0; /* return an empty string */ return t; } else t_ = ( unsigned char* ) (t+i); /* t_ points to the first non-whitespace */ /* Process the rear end of the string */ i=l-1; while (*(tc_+i) <= ' ') i--; i++; /* Index of the 1st whitespace at the rear end of the text */ *(t+i) = ( char ) 0; /* change into ASCIIZ */ l = strlen((char*) t_) + 1; /* Length clean string + ASCIIz */ memmove(t, t_, l); /* Secure even when overlapping */ return t; } Unfortunatedly the preview of the code looks awfull, like it has been handled by alltrim itself. Regards, Henk 21cm: the universal wavelength of neutral hydrogen

        G 1 Reply Last reply
        0
        • H henk21cm

          G'day George, dBaseIII had a popular function for this purpose, alltrim. In the mid nineties i needed that function in C and wrote the following implementation, plain C code. char *alltrim( char *t) { unsigned char *t_, *tc_; int i=0, l; l = strlen(t); tc_ = (unsigned char*) t; /* Remove all spaces before any usefull characters */ while (*(tc_+i) <= ' ' && i < l) i++; if (i==l) /* Everything is white space */ { *t = (char) 0; /* return an empty string */ return t; } else t_ = ( unsigned char* ) (t+i); /* t_ points to the first non-whitespace */ /* Process the rear end of the string */ i=l-1; while (*(tc_+i) <= ' ') i--; i++; /* Index of the 1st whitespace at the rear end of the text */ *(t+i) = ( char ) 0; /* change into ASCIIZ */ l = strlen((char*) t_) + 1; /* Length clean string + ASCIIz */ memmove(t, t_, l); /* Secure even when overlapping */ return t; } Unfortunatedly the preview of the code looks awfull, like it has been handled by alltrim itself. Regards, Henk 21cm: the universal wavelength of neutral hydrogen

          G Offline
          G Offline
          George_George
          wrote on last edited by
          #4

          Thanks Henk, I think you mean there is no built-in function. regards, George

          1 Reply Last reply
          0
          • C Christian Graus

            http://www.codeproject.com/vcpp/stl/stdstringtrim.asp[^]

            Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            G Offline
            G Offline
            George_George
            wrote on last edited by
            #5

            Hi Christian, It is implemented by find_first_not_of and find_last_not_of of STL string, no built-in functions. regards, George

            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