Sign in to follow this  
Followers 0
blitzer99

New string TRIM() functions

15 posts in this topic

My thanks to the guys on my forum topic "Enhancement to string TRIM functions" who provided the inspiration (and a lot of the code) :) for four new string TRIM() functions: _LTRIM() _RTRIM(), _ALLTRIM() and _TRIM().

These functions trim unwanted characters from the left, right or both ends of a character string. The default characters trimmed are spaces but more than one contiguous character can be trimmed it once. For example back slashes and spaces my be trimmed together.

The functions that achieve this result are included in the attached file along with installation instructions, reproduced below.

==================================================================

Purpose: Implements new functions _RTRIM(), _LTRIM(), _ALLTRIM(), _TRIM()

To implement functions equivalent to the old dBase functions that removed spaces

from the, respectively, right, left and both left and right of a character string.

The function _TRIM() is equivalent to _RTRIM() and is provided for consistency with

the old dBase standard.

These four functions remove contiguous characters from the left and/or right of a character string

but not those that are within the string. For example _RTRIM("Mary had a little lamb ")

will become "Mary had a little lamb" - the spaces inside the string are not removed.

Syntax: ResultString = <FunctionName>( string1, string2 )

<FunctionName> as above: _RTRIM(), _LTRIM(), _ALLTRIM(), _TRIM()

string1 is the character string to examine.

string2 is a list of the characters to be examined for; if omitted it defaults to the standard space character, chr(32).

ResultString is the string returned by the function with all characters in string2 trimmed from the left and/or right

of string1.

If string2 = "%%whs%%" all white space characters will be trimmed from string1.

Whitespace includes Chr(9) thru Chr(13) which are HorizontalTab, LineFeed, VerticalTab, FormFeed, and arriageReturn.

Whitespace also includes the standard space character.

ResultString will be shorter than string1 by an amount equal to the number of characters trimmed from string1.

These functions are not case sensitive, e.g. "m" in string2 will trim both "M" and "m" from string1.

On any error condition ResultString will be equal to string1.

Examples: _RTRIM("Mary had a little lamb ") -> "Mary had a little lamb"

_RTRIM("Mary had a little giraffe xyxyxyz", "xyz") -> "Mary had a little giraffe " (note space at end has not been removed).

_RTRIM("Mary had a little giraffe xyxyxyz", " xyz") -> "Mary had a little giraffe" (now the same space has been removed because

a space is now included in string2).

_RTRIM("Mary had a little giraffe xyxbyxyz", "xyz") -> "Mary had a little giraffe xyxb" (only the x, y and z following the "b"

will be removed because the "b" is not in string2.

To use: 1. Copy this script to the "Include" folder under the AutoIt programs folder.

(for example: c:\program files\autoit3\include\myfunctions.au3 )

2. In your Autoit3 script have following statement:

#include <myfunctions.au3>

3. "myfunctions" can be renamed to anything you want.

Disclaimer: Thoroughly tested and debugged, but use entirely at your own risk.

Usage automatically acknowledges acceptance of this condition.

MyFunctions.zip

1 person likes this

Computers don't solve problems, they just rearrange them.New string TRIM() functions for AutoIt3

Share this post


Link to post
Share on other sites



164 downloads and not one reply?

Thanks for this, it makes easy work of cleaning up dirty strings! >_<

Share this post


Link to post
Share on other sites

There isn't much to say. They work. They can be useful. Good job.


[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post


Link to post
Share on other sites

Thanks!!

Share this post


Link to post
Share on other sites

A belated thx!!

Share this post


Link to post
Share on other sites

Perfect :)

Thank you!


I am just a hobby programmer, and nothing great to publish right now.

Share this post


Link to post
Share on other sites

Another happy dl'er - Excellent Job!!! Thanks ;)

Share this post


Link to post
Share on other sites

Thank you ! I was just looking for this function in AutoIt ;)

Share this post


Link to post
Share on other sites

Very nice.  Thank you. :D

Share this post


Link to post
Share on other sites

Try to trim this:

"Model: Dell Optiplex 3010", "Model: "

 

With that I get a trim off Model: Dell Opt.


Msgbox(0, "Hate", "Just hate it when I post a question and find my own answer after a couple tries. But if I don't post the question, I can't seem to resolve it at all.")

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

If the OP was 20 years old while posting this, he's about 30 now.

Edited by D4RKON3

Share this post


Link to post
Share on other sites

asianqueen,

Is that _LTrim() that you are using? Note that _Trim() instead does a RTrim() in which nothing is removed as nothing matches the pattern on the right side.

With this

$result = _LTRIM("Model: Dell Optiplex 3010", "Model: ")
MsgBox(0, '$result', $result)

I get returned

ptiplex 3010

Every character from the left up to the p is in the 2nd parameter so it is trimmed.

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

shouldn't it trim only Model: with the space only? Why does it trim the O along with it?

reality it trim off "Model: " then leave out Dell Optiplex 3010. So $result should be "Dell Optiplex 3010"

Edited by asianqueen

Msgbox(0, "Hate", "Just hate it when I post a question and find my own answer after a couple tries. But if I don't post the question, I can't seem to resolve it at all.")

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

The 2nd parameter is treated as a set of characters, not a string. So "Model: " could be " :ledoM" or any other sequence. All of the characters in "Model: Dell O" exist in "Model: " and vice versa.

Similar functions can be found in SQLite here. i.e.

ltrim(X)
ltrim(X,Y)

The ltrim(X,Y) function returns a string formed by removing any and all characters that appear in Y from the left side of X. If the Y argument is omitted, ltrim(X) removes spaces from the left side of X.

Note: the phase "removing any and all characters that appear in Y from the left side of X".

Thus any character in the set of characters will be removed. Similar functions in behavior.

Edit: 1st post mentions case insensitivity in matching.

Edited by MHz

Share this post


Link to post
Share on other sites

I see... that make senses. Instead, I know Model: is constant so I did a left trim by counts including space. Works great!


Msgbox(0, "Hate", "Just hate it when I post a question and find my own answer after a couple tries. But if I don't post the question, I can't seem to resolve it at all.")

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0