blitzer99 Posted July 31, 2005 Share Posted July 31, 2005 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 Remie9, IgImAx and KeeperOfTheReaper 3 Computers don't solve problems, they just rearrange them.New string TRIM() functions for AutoIt3 Link to comment Share on other sites More sharing options...
ModemJunki Posted August 7, 2009 Share Posted August 7, 2009 164 downloads and not one reply? Thanks for this, it makes easy work of cleaning up dirty strings! >_< Always carry a towel. Link to comment Share on other sites More sharing options...
dantay9 Posted August 7, 2009 Share Posted August 7, 2009 There isn't much to say. They work. They can be useful. Good job. Link to comment Share on other sites More sharing options...
blitzkrg Posted August 7, 2009 Share Posted August 7, 2009 Thanks!! Link to comment Share on other sites More sharing options...
dixonpete Posted October 10, 2011 Share Posted October 10, 2011 A belated thx!! Link to comment Share on other sites More sharing options...
Myicq Posted December 14, 2011 Share Posted December 14, 2011 Perfect Thank you! I am just a hobby programmer, and nothing great to publish right now. Link to comment Share on other sites More sharing options...
john7a8 Posted May 3, 2012 Share Posted May 3, 2012 Another happy dl'er - Excellent Job!!! Thanks Link to comment Share on other sites More sharing options...
BjornA Posted May 21, 2012 Share Posted May 21, 2012 Thank you ! I was just looking for this function in AutoIt Link to comment Share on other sites More sharing options...
wolflake Posted April 21, 2014 Share Posted April 21, 2014 Very nice. Thank you. Link to comment Share on other sites More sharing options...
Queener Posted May 14, 2014 Share Posted May 14, 2014 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 More sharing options...
FaridAgl Posted May 14, 2014 Share Posted May 14, 2014 (edited) If the OP was 20 years old while posting this, he's about 30 now. Edited May 14, 2014 by D4RKON3 http://faridaghili.ir Link to comment Share on other sites More sharing options...
MHz Posted May 15, 2014 Share Posted May 15, 2014 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 More sharing options...
Queener Posted May 15, 2014 Share Posted May 15, 2014 (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 May 15, 2014 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 More sharing options...
MHz Posted May 15, 2014 Share Posted May 15, 2014 (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 May 15, 2014 by MHz Link to comment Share on other sites More sharing options...
Queener Posted May 27, 2014 Share Posted May 27, 2014 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now