|
-
Aug 21st, 2001, 03:26 AM
#1
Thread Starter
PowerPoster
Trim Function in C/VC++
Can anyone show me the available Trim function in C/VC++ or any equavalent function 
regards,
-
Aug 21st, 2001, 04:53 AM
#2
transcendental analytic
Dunno if there is one, here's something i made just for you 
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;
}
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 21st, 2001, 05:23 AM
#3
Thread Starter
PowerPoster
Thx Keda for you prompt reply. Mean while, I also get some sample code from PlanetSouceCode 
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;
}
regards,
-
Aug 21st, 2001, 05:54 AM
#4
transcendental analytic
that's nullchars not spaces, besides you can hardly use them in conjunction to function as Trim.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 21st, 2001, 01:17 PM
#5
Monday Morning Lunatic
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;
}
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
-
Aug 21st, 2001, 01:49 PM
#6
transcendental analytic
Dunno, but looks very heavy to me :/
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 21st, 2001, 01:52 PM
#7
Monday Morning Lunatic
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
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
-
Aug 21st, 2001, 02:16 PM
#8
transcendental analytic
oh yeah and it's so heavy it makes me throw up
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 21st, 2001, 02:28 PM
#9
Monday Morning Lunatic
Git 
If I could really be bothered I might write a faster one
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
-
Aug 21st, 2001, 03:51 PM
#10
transcendental analytic
hehe, you should learn to use the power in C++
May the Force be with you
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 21st, 2001, 06:05 PM
#11
Monday Morning Lunatic
Speed wasn't a priority in that one, but I accept your challenge
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
-
Aug 21st, 2001, 10:44 PM
#12
Thread Starter
PowerPoster
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
-
Aug 23rd, 2001, 10:09 AM
#13
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
-
Aug 23rd, 2001, 10:33 AM
#14
transcendental analytic
kinda old habit since i used vb
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
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
|