Sign in to follow this  
Followers 0
CodeMaster Rapture

New string function!

13 posts in this topic

#1 ·  Posted (edited)

Hi all,

I was recently creating an app and needed a function to strip ASCII characters from a string. I couldn't find a function that suited my needs directly so I made my own and decided to share it with everyone. It is simple and effective.

Updated 5-19-05 @ 8:17 EST

Update Notes:

Simplified the loop.

Fixed the error checking.

#cs
***********************************************************************************************
 AutoIt Version: 3.1.0
 Author: CodeMaster Rapture

 Script Function:
    Remove ASCII characters from a string.

 Usage:
    StringStrip( String, Chars [,Occurrances, Case Sensitive])
 Parameters:
    String ------------------ The string you want stripped.
    Chars ------------------- The Characters you want removed. I.E. "aAz34!@4"
    Occurrances ------------- How many occurrances of each character to remove. Default is
    ------------------------- 0 which removes all. If a negative number is entered it is
    ------------------------- treated as 0.
    Case Sensitive ---------- 1 will remove only the case of the letters the user specifies
    ------------------------- 0 remove both lowercase and uppercase letters. 0 is default.
***********************************************************************************************
#ce

Func StringStrip($String, $Chars, $Count = 0, $CaseSensitive = 0)

;Error Checking
    If ($String == "") Then         Return $String
    If ($Chars == "") Then            Return $String
    If NOT (IsInt($CaseSensitive)) Then    $CaseSensitive = Number($CaseSensitive);Returns 0 or the number
    If ($CaseSensitive > 1) Then        $CaseSensitive = 1
    If NOT (IsInt($Count)) Then        $Count = Number($Count);Returns 0 or the number

;Let's make this into an array for looping
    $Chars = StringSplit($Chars,"")
    
    For $loop = 0 To $Chars[0]
        $String = StringReplace($String, $Chars[$loop],"", $Count, $CaseSensitive)
    Next

    Return $String
EndFunc

Enjoy!

Edited by CodeMaster Rapture

Share this post


Link to post
Share on other sites



I think this will be a very useful udf. Thank you.


.

Share this post


Link to post
Share on other sites
:(

Share this post


Link to post
Share on other sites

If ($CaseSensitive < 0) Then $CaseSensitive = 0

Is probably not needed

For $loop = 0 To (UBound($Chars) - 1)

Should be changed to

For $loop = 0 To $Chars[0]


"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I think I'd be tempted to change this:

If ($CaseSensitive < 0) Then    $CaseSensitive = 0
    If ($CaseSensitive > 1) Then    $CaseSensitive = 1

to this:

If (($CaseSensitive<>0) AND ($CaseSensitive<>1)) Then $CaseSensitive = 0

That way some doofus won't break your UDF like this:

StringStrip("XYZ123abc", "x", 0, "zero" )...

Other than that - THANKS! I'm already copying it to my Includes directory. :(

[EDIT] Oh, and you may want to add this: $count = $count + 0 for the same reason...

Edited by mrider

How's my riding? Dial 1-800-Wait-There

Trying to use a computer with McAfee installed is like trying to read a book at a rock concert.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

If ($CaseSensitive < 0) Then    For $loop = 0 To (UBound($Chars) - 1)

Should be changed to

For $loop = 0 To $Chars[0]

<{POST_SNAPBACK}>

Is really the same but.....

Are you sure it shouldn't be :

For $loop = 1 To $Chars[0]
or

For $loop = 1 To (UBound($Chars) - 1)
Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Thank you everyone for the responses. I'll be sure to test it out more extensively. It's too bad you can't declare variables as specific data types (or atleast I dunno how).

As for why I used UBound($Chars)-1, most string arrays in C/C++ (assuming that's what this language is based off of) start at subscript 0, so if we define it as having 100 elements, then the range is from 0-99, not 1-100.

I'll post the fixed version of this UDF here in a few minutes.

Glad everyone likes it!

CMR

Share this post


Link to post
Share on other sites

For $loop = 0 To (UBound($Chars) - 1)

Should be changed to

For $loop = 0 To $Chars[0]

<{POST_SNAPBACK}>

Why use $Chars[0]? Won't that be an illegal loop type? $Chars[0] would be the first character in the array, right?

Share this post


Link to post
Share on other sites

JdeB correctly points out that it should be:

For $loop = 1 to $Chars[0]

If you read the contract of StringSplit, you'll see that it returns the number of elements in the array in element 0, followed by the split things in element 1 to element n.

This goes back to the days when AutoIt didn't have an easy way to find an array's boundaries (AutoIt2??).


How's my riding? Dial 1-800-Wait-There

Trying to use a computer with McAfee installed is like trying to read a book at a rock concert.

Share this post


Link to post
Share on other sites

JdeB correctly points out that it should be:

For $loop = 1 to $Chars[0]

If you read the contract of StringSplit, you'll see that it returns the number of elements in the array in element 0, followed by the split things in element 1 to element n.

This goes back to the days when AutoIt didn't have an easy way to find an array's boundaries (AutoIt2??).

<{POST_SNAPBACK}>

Ooooh, ok. That makes sense. We all learn something new everyday. So does UBound ignore $array[0]?

Share this post


Link to post
Share on other sites

Ooooh, ok. That makes sense. We all learn something new everyday. So does UBound ignore $array[0]?

<{POST_SNAPBACK}>

Helpfile says :

"...the first element ($array[0]) contains the number of strings returned..."

$array = StringSplit("h,e,l,g,e",",")
MsgBox(64,"","$array[0] returns " & $array[0])

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

So does UBound ignore $array[0]?

Sorry, no. I guess we can't have our cake and eat it too...

$split=StringSplit("abc", "");

$split[0] == 3

$split[1] == "a"

$split[2] == "b"

$split[3] == "c"

UBound( $split ) == 4

At least it's not some funky Pascal thing where the array can start at element 7 and end at element 34 and have six elements though :(

Edited by mrider

How's my riding? Dial 1-800-Wait-There

Trying to use a computer with McAfee installed is like trying to read a book at a rock concert.

Share this post


Link to post
Share on other sites

And since we're on the subject - you should probably get in the habit of checking the return value contracts of all functions which return an array. At this point the concept of element 0 containing the number of elements in the array is fairly ubiquitous.


How's my riding? Dial 1-800-Wait-There

Trying to use a computer with McAfee installed is like trying to read a book at a rock concert.

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