Thread: string tips

  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
    28,413
    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.
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 11:03 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM