Can anyone show me the available Trim function in C/VC++ or any equavalent function :(
regards,
Printable View
Can anyone show me the available Trim function in C/VC++ or any equavalent function :(
regards,
Dunno if there is one, here's something i made just for you :p
PHP Code:void Trim(char*,char*,char*);
void Trim(char* infirst, char* inlast,char* out){
for(;(infirst<inlast)&&(*inlast==0);--inlast);
for(;(infirst<inlast)&&(*infirst==32);++infirst);
for(;(infirst<inlast)&&(*inlast==32);--inlast);
for(;(infirst<=inlast);out++)*out=*infirst++;
*out=0;
}
int main(int argc, char* argv[])
{
char text[8]=" Chris ";
char text2[8];
Trim(&text[0],&text[7],&text2[0]);
cout << text2;
return 0;
}
Thx Keda for you prompt reply. Mean while, I also get some sample code from PlanetSouceCode :p
regards,PHP Code:wchar_t* LTrim(wchar_t* s)
{
//**************************************
//
// Name: strrtrim
// Description:Removes trailing spaces from the end of a string. See "trim" (Rename to "LTrim") to remove leading spaces.
// By: Man In Limbo
//
// Inputs:the string
//
// Returns:the trimmed string
//
// This code is copyrighted and has// limited warranties.Please see [url]http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.233/lngWId.3/qx/vb/scripts/ShowCode.htm[/url] ::for details.
//
//**************************************
// strrtrim : removes trailing spaces from the end of a string
int i;
if (s)
{
i = wcslen(s);
while ((--i)>0 && iswspace(s[i]) ) s[i]=0;
}
return s;
}
wchar_t* RTrim(wchar_t* s)
{
//**************************************
//
// Name: trim
// Description:Strips spaces from front of string. Also see strrtrim (Rename to "LTrim")to remove the spaces off the end.
// By: Man In Limbo
//
// Inputs:The String
//
// Returns:The Trimmed String.
//
// This code is copyrighted and has// limited warranties.
// Please see [url]http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.232/lngWId.3/qx/vb/scripts/ShowCode.htm[/url] ::for details.
//
//**************************************
// trim : scan over spaces from the front of a string.
if (s)
{
while (iswspace(*s)) s++;
}
return s;
}
that's nullchars not spaces, besides you can hardly use them in conjunction to function as Trim.
Is this allowed?Code:tstring Trim(tstring sIn) {
int i, start = -1, end = -1;
for(i = 0; i < sIn.length(); i++) {
if(_istspace(sIn[i])) start = i;
else {
sIn = sIn.substr(i, sIn.length() - i);
break;
}
}
for(i = sIn.length()-1; i >= 0; i--) {
if(_istspace(sIn[i])) end = i;
else {
sIn = sIn.substr(0, i+1);
break;
}
}
return sIn;
}
Dunno, but looks very heavy to me :/
A bit, yeah. The advantage is that it works a treat, stripping off newlines, tabs, and spaces :) And it calls .substr a maximum of twice :)
oh yeah and it's so heavy it makes me throw up :)
Git :p
If I could really be bothered I might write a faster one ;)
hehe, you should learn to use the power in C++
May the Force be with you ;)
Speed wasn't a priority in that one, but I accept your challenge ;)
:D I think those written app with C++ is 'coz of the speed & power of control everthing. Juz like me choose VC++ to write app in WinCE.
by the way, thanks for both of you sample code & it help very much.
Quote:
Originally posted by parksie
Speed wasn't a priority in that one, but I accept your challenge ;)
tell me one thing kedaman
why do you keep using the ascii codes of the letters. It's not as if ' ' or '\0' was any slower (resolved by the compiler) and it is far more readable.
All the buzzt
CornedBee
kinda old habit since i used vb