|
-
Jul 26th, 2002, 12:46 PM
#1
Thread Starter
Ya ya Baby!!!Me is Back
TRIM in c++
I know that in java and in VB string have something .trim() but in C++ what is the name for "trim"?
-
Jul 26th, 2002, 01:25 PM
#2
Thread Starter
Ya ya Baby!!!Me is Back
trim take out space... like " allo " will be : "allo"
-
Jul 26th, 2002, 01:32 PM
#3
Frenzied Member
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;
i = stString->length()-1; //Set The Counter To The String Length -1
while (stString->substr(i,1) == " ") //While There Is Still " "
{
*stString = stString->substr(0, i); //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
i = 0; //Setup The Counter VAR
while (stString->substr(i,1) == " ") //While " "
i++; //Increase Counter
if (i == 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

-
Jul 26th, 2002, 04:10 PM
#4
Monday Morning Lunatic
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
-
Jul 26th, 2002, 04:12 PM
#5
Frenzied Member
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

-
Jul 26th, 2002, 04:53 PM
#6
Monday Morning Lunatic
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
-
Jul 26th, 2002, 05:33 PM
#7
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|