Jump to content

New string TRIM() functions


blitzer99
 Share

Recommended Posts

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

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

Link to comment
Share on other sites

  • 4 years later...
  • 2 years later...
  • 2 months later...
  • 4 months later...
  • 3 weeks later...
  • 1 year later...
  • 4 weeks later...

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.")
Link to comment
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.

Link to comment
Share on other sites

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.")
Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • 2 weeks later...

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.")
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...