Results 1 to 7 of 7

Thread: TRIM in c++

  1. #1

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362

    TRIM in c++

    I know that in java and in VB string have something .trim() but in C++ what is the name for "trim"?

  2. #2

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    trim take out space... like " allo " will be : "allo"

  3. #3
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    I am going to guess you want to trim a string. Here is the solution I came up with

    PHP Code:
    /*##############################################################################################*/

    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    //| Function:    Trim()
    //|                    Overload Function
    //| In:            string *stString
    //|                    In String
    //| Return:        None
    //| Notes:        This Function Trims Spaces From The Begining And The End Of A String
    //|                This Function Was Created By jim mcnamara on VB-World.net Forums
    //| Example:    string p = "    TTTT    ";
    //|                Trim(p);            //Should Chang p To "TTTT"
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    void Trim(string *stString)
    {
        
    RTrim(stString);
        if (
    stString->length())
            
    LTrim(stString);
    }


    /*##############################################################################################*/

    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    //| Function:    RTrim()
    //|                    Overload Function
    //| In:            string stString
    //|                    In String
    //| Return:        None
    //| Notes:        This Function Trims Spaces From The End Of A String
    //| Example:    string p = "TTTT    ";
    //|                RTrim(p);            //Should Chang p To "TTTT"
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    void RTrim(string *stString)
    {
    /*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
        
    int i;                            //Count VAR
    /*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/

        
    if (!stString->length())                    //If There Is No Length
            
    return;

        
    stString->length()-1;                    //Set The Counter To The String Length -1

        
    while (stString->substr(i,1) == " ")        //While There Is Still " "
        
    {
            *
    stString stString->substr(0i);        //Remove Off The End Of The String
            
    i--;                                    //Advance The Counter
        
    }

    }

    /*##############################################################################################*/

    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    //| Function:    LTrim()
    //|                    Overload Function
    //| In:            string *stString
    //|                    In String
    //| Return:        None
    //| Notes:        This Function Trims Spaces From The Front Of A String
    //| Example:    string p = "    TTTT";
    //|                LTrim(p);            //Should Chang p To "TTTT"
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    void LTrim(string *stString)
    {
    /*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
        
    int i;                            //Count VAR
        
    string buf;                        //Temp String VAR
    /*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/

        
    if (!stString->length()) return;                        //If The Length Is 0

        
    0;                                                    //Setup The Counter VAR

        
    while (stString->substr(i,1) == " ")                    //While " "
            
    i++;                                                //Increase Counter

        
    if (== stString->length())                            //If The Whole String Is " "
        
    {
            *
    stString "";                                        //Return Blank String
            
    return;
        }

        try
        {
            *
    stString stString->substr(i,stString->length()-i);    //Send The String Back Without The Spaces
        
    }
        catch(...)
        {
            *
    stString "";                                        //Return Blank String
            
    return;
        }


    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Two comments:

    1. References rather than pointers, safer and easier

    2. In my version of a trim function, I used isspace rather than checking for just spaces...personal preference but you might find it useful.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Can you give me a quicky example of how you would use a referance?
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    
    using namespace std;
    
    string trim(const string &str) {
    	string::const_iterator start, end;
    
    	for(start = str.begin(); start != str.end(); ++start) {
    		if(!isspace(*start)) break;
    	}
    
    	for(end = str.end() - 1; end != str.begin(); --end) {
    		if(!isspace(*end)) break;
    	}
    
    	return string(start, end + 1);
    }
    
    int main() {
    	string s("    \t\n  Hello there      ");
    	cout << "blah:" << trim(s) << ":thingie" << endl;
    }
    That's a method that returns a new string, to trim it in-place, try this:
    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    
    using namespace std;
    
    void trim(string &str) {
    	string::iterator start, end;
    
    	for(start = str.begin(); start != str.end(); ++start) {
    		if(!isspace(*start)) break;
    	}
    
    	for(end = str.end() - 1; end != str.begin(); --end) {
    		if(!isspace(*end)) break;
    	}
    
    	str.assign(start, end + 1);
    }
    
    int main() {
    	string s("    \t\n  Hello there      ");
    	trim(s);
    	cout << "blah:" << s << ":thingie" << endl;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    ahh, I see. Never thought of that.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width