I know that in java and in VB string have something .trim() but in C++ what is the name for "trim"?
Printable View
I know that in java and in VB string have something .trim() but in C++ what is the name for "trim"?
trim take out space... like " allo " will be : "allo"
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;
}
}
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.
Can you give me a quicky example of how you would use a referance?
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;
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;
}
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;
}
ahh, I see. Never thought of that.