QB64 Phoenix Edition
DAY 027: _TRIM$ - Printable Version

+- QB64 Phoenix Edition (https://staging.qb64phoenix.com)
+-- Forum: Official Links (https://staging.qb64phoenix.com/forumdisplay.php?fid=16)
+--- Forum: Learning Resources and Archives (https://staging.qb64phoenix.com/forumdisplay.php?fid=13)
+---- Forum: Keyword of the Day! (https://staging.qb64phoenix.com/forumdisplay.php?fid=49)
+---- Thread: DAY 027: _TRIM$ (/showthread.php?tid=1246)



DAY 027: _TRIM$ - Pete - 12-08-2022

Space, the final frontier, and when you're sick of space on both sides of your string, use _TRIM$.

_TRIM$ simply removes any leading and/or trailing spaces from any string.

SYNTAX: _TRIM$(mytext$) and can also be used as: _TRIM$("   my text   ")

_TRIM$ is the QB64 answer to, "What do you get when you put LTRIM$ + RTRIM$ together?" Well, until _TRIM$ came along it was LTRIM$(RTRM$(mystring$)). Note: LTRIM$ removes leading spaces, spaces to the "left" and RTRIM$ removes trailing space, spaces to the right, and _TRIM$ removes both.


If there are no spaces, _TRIM$ simply does nothing.

_TRIM$ can be combined with STR$(), which converts a number to a string and removes the trailing space. So why do we need _TRIM? The answer is to get rid of the leading space the system uses to reserve space for a possible negative sign in front of a number even after STR$() is used to convert it to a string.

So while PRINT STR$(-1) is "-1", PRINT STR$(1) would be " 1". To get rid of that leading space we can code either: PRINT LTRIM$(STR$(1)) or PRINT _TRIM$(STR$(1)). Of course most of the time a number will be represented by a variable, so we usually code: MyNumber = 1: MyNumber$ = _TRIM$(STR$(a)).

Code: (Select All)
a = -1
PRINT "|"; a; "|" ' Has one trailing space.
PRINT "|"; STR$(a); "|" ' Chops the trailing space when converting to a string.
PRINT "|"; _TRIM$(STR$(a)); "|" ' Actually not needed here because of the negative number value.
PRINT
a = 1
PRINT "|"; a; "|" ' Has one leading space and one trailing space.
PRINT "|"; STR$(a); "|" ' Chops the trailing space when converting to a string.
PRINT "|"; _TRIM$(STR$(a)); "|" ' Chops the remaining leading space.

Another use is when a DIM statement if made to produce a "fixed" string. A fixed string defines the string length and creates trailing spaces if the string is smaller the dim size created.

Example:

Steve's Spreadsheet
Code: (Select All)
DIM a as STRING * 10 ' All strings named a will be 10 bytes long.
FOR i = 1 TO 3 ' (Not 2 B confused with 1, 2, 3.)  :D
READ a
PRINT "|";a;"|", LEN(a)
PRINT "|";_TRIM$(a);"|", LEN(_TRIM$(a)) ' Here we combine _TRIM$ with LEN() to output the length of our trimmed string.
NEXT

' Steve's sheet spreaders...
DATA Horse,Pig,Mule

_TRIM$ is often used in parsing routines to compare strings as apples to apples, instead of apples to apples with leading and trailing spaces.

So how about some more use examples? Feel free to post yours...


RE: DAY 027: _TRIM$ - mnrvovrfc - 12-08-2022

It's a good thing QB64(PE) doesn't have "ANY" clause, that's darned confusing.

The "_TRIM$()" should be expanded to remove characters other than CHR$(32). I was going to propose only the control characters but that is useful pretty much to replicate "strings" console program that is usually packaged with gcc or with MinGW.

LOL before I got ahold of "strings" program I used to load EXE files into Wordpad to read a few amusing messages, such as the runtime error messages out of the old M$ C runtime library. "Virtual function call" and "This indicates a bug in your application" were among those messages. There was a program I purchased which created a log file while running. I peeked into its EXE file for all possible combinations and found a funny message. It was very unlikely to be triggered, during my use of that application unless I were rich and cared less about programming than about owning as much as possible to get along better with people found in the forum I used to belong to.

Another thing is to load "reaper.exe", which belonged to v1 or v2, into Wordpad or put it through "strings" program. Whoever tried to fabricate a license key and cursed out the program for not working properly could have found that program cursing at him/her back! Exclamation


RE: DAY 027: _TRIM$ - Dav - 12-08-2022

Man, I totally forgot about the _TRIM$ command, I've still been using a LTRIM + RTRIM combination.  I need to make a habit of browsing through the keyword list now and then.  Thanks for keeping up the daily keyword posts, Pete. 

Right now I'm trimming  a Christmas  _TRIM$("       tree.       "), and this morning trimmed my_TRIM$("    beard.      ").

- Dav


RE: DAY 027: _TRIM$ - SMcNeill - 12-08-2022

(12-08-2022, 04:08 PM)Dav Wrote: Man, I totally forgot about the _TRIM$ command, I've still been using a LTRIM + RTRIM combination.  I need to make a habit of browsing through the keyword list now and then.  Thanks for keeping up the daily keyword posts, Pete. 

Right now I'm trimming  a Christmas  _TRIM$("       tree.       "), and this morning trimmed my_TRIM$("    beard.      ").

- Dav

I've went the complete opposite way, @Dav.  I've gotten so used to _TRIM$, that I've more or less forgotten about LTRIM$ and RTRIM$.   When I see them in someone's code now, my first thought is they're LEFT$ or RIGHT$, and I really have to do more than just glance across the code for it to register to me that they're not those commands at all!  LOL!


RE: DAY 027: _TRIM$ - SpriggsySpriggs - 12-08-2022

I pretty much always use _TRIM$ instread of LTRIM$ or RTRIM$, mainly because I want to be absolutely sure I don't have any leading or trailing spaces. Instead of doing one or the other, I figure doing both is best.


RE: DAY 027: _TRIM$ - vince - 12-08-2022

nice keyword of the day choice, Steve.  I believe that Visual Basic introduced the TRIM$ (sans underscore) keyword and it was added to QB64 by Fellippe back when Fellippe and Steve were having a 'who can add more keywords to the language' contest


RE: DAY 027: _TRIM$ - Pete - 12-08-2022

(12-08-2022, 09:50 PM)vince Wrote: nice keyword of the day choice, Steve.  I believe that Visual Basic introduced the TRIM$ (sans underscore) keyword and it was added to QB64 by Fellippe back when Fellippe and Steve were having a 'who can add more keywords to the language' contest

@vince

You're welcome Vince, but my name's Pete. Steve is my evil twin.


RE: DAY 027: _TRIM$ - vince - 12-08-2022

Apologies for the misunderstanding, Pete.  I thought that Steve was running a list of some sort, perhaps in building up more content for The QB64 Bible.  Either way, nice job to the both of you, this series has been very helpful and the guides are of really high quality


RE: DAY 027: _TRIM$ - SMcNeill - 12-08-2022

(12-08-2022, 11:24 PM)vince Wrote: Apologies for the misunderstanding, Pete.  I thought that Steve was running a list of some sort, perhaps in building up more content for The QB64 Bible.  Either way, nice job to the both of you, this series has been very helpful and the guides are of really high quality

https://staging.qb64phoenix.com/showthread.php?tid=1059 <-- I was just doing a non-GL Randomizer.  Wink


RE: DAY 027: _TRIM$ - Pete - 12-09-2022

(12-08-2022, 11:24 PM)vince Wrote: Apologies for the misunderstanding, Pete.  I thought that Steve was running a list of some sort, perhaps in building up more content for The QB64 Bible.  Either way, nice job to the both of you, this series has been very helpful and the guides are of really high quality

@vince

No worries. Steve has been busy with other things for the past few days, so I jumped in. Steve uses a randomizer, me, I just pull them out of my ASCII. Not quite the same as Steve's more sophisticated Magic 8-Ball method, but I find if I shake it, it sends the gals running... unfortunately, mostly in the opposite direction.

Pete