string tips

This is a discussion on string tips within the C++ Programming forums, part of the General Programming Boards category; Anyone got a good way to do this without having to use a c-string (and <cstring>): Code: #include <iostream> #include ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    string tips

    Anyone got a good way to do this without having to use a c-string (and <cstring>):
    Code:
    #include <iostream>
    #include <string>
    #include <cstring>
    using namespace std;
    
    void stripWS(string &str) {
    	int len = str.size();
    	char copy[len+1], *p;
    	strcpy(copy,str.c_str());
    	p = &copy[len-1];
    	while (*p == ' ' || *p == '\t') *(p--) = '\0';
    	p = copy;
    	while (*p == ' ' || *p == '\t') *p++;
    	str = p;
    }
    
    
    int main() {
    	string test("\t\t so what\t   ");
    
    	cout << "->" << test << "<-\n";
    	stripWS(test);
    	cout << "->" << test << "<-\n";
    
    	return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    I have not tested it, but perhaps something like this will work:
    Code:
    void stripWS(string& str) {
        const string::size_type first = str.find_first_not_of(" \t");
        if (first != string::npos)
        {
            const string::size_type last = str.find_last_not_of(" \t");
            str = str.substr(first, last - first + 1);
        }
        else
        {
            str.clear(); // The string only comprises of whitespace.
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yeah, that's the kind of thing I was thinking of. Seems to work too. Thanks!
    Last edited by MK27; 03-22-2010 at 10:25 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21