Click to See Complete Forum and Search --> : Trim Function in C/VC++
Chris
Aug 21st, 2001, 03:26 AM
Can anyone show me the available Trim function in C/VC++ or any equavalent function :(
regards,
kedaman
Aug 21st, 2001, 04:53 AM
Dunno if there is one, here's something i made just for you :p
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;
}
Chris
Aug 21st, 2001, 05:23 AM
Thx Keda for you prompt reply. Mean while, I also get some sample code from PlanetSouceCode :p
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 http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.233/lngWId.3/qx/vb/scripts/ShowCode.htm ::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 http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.232/lngWId.3/qx/vb/scripts/ShowCode.htm ::for details.
//
//**************************************
// trim : scan over spaces from the front of a string.
if (s)
{
while (iswspace(*s)) s++;
}
return s;
}
regards,
kedaman
Aug 21st, 2001, 05:54 AM
that's nullchars not spaces, besides you can hardly use them in conjunction to function as Trim.
parksie
Aug 21st, 2001, 01:17 PM
Is this allowed?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;
}
kedaman
Aug 21st, 2001, 01:49 PM
Dunno, but looks very heavy to me :/
parksie
Aug 21st, 2001, 01:52 PM
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 :)
kedaman
Aug 21st, 2001, 02:16 PM
oh yeah and it's so heavy it makes me throw up :)
parksie
Aug 21st, 2001, 02:28 PM
Git :p
If I could really be bothered I might write a faster one ;)
kedaman
Aug 21st, 2001, 03:51 PM
hehe, you should learn to use the power in C++
May the Force be with you ;)
parksie
Aug 21st, 2001, 06:05 PM
Speed wasn't a priority in that one, but I accept your challenge ;)
Chris
Aug 21st, 2001, 10:44 PM
: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.
Originally posted by parksie
Speed wasn't a priority in that one, but I accept your challenge ;)
CornedBee
Aug 23rd, 2001, 10:09 AM
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
kedaman
Aug 23rd, 2001, 10:33 AM
kinda old habit since i used vb
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.