Sign in to follow this  
Followers 0
Bowmore

Strip any characters from a string function

7 posts in this topic

#1 ·  Posted (edited)

I thought this function may be useful to some people. It works in almost the same way as the core AutoIt function StringStripWS. The difference is that you can specify any characters to be stripped.

Note: The function is case sensitive

;========================================================================================================
;
; Function Name:     _StringStripChr($sString_In, $sChr, $iFlags = 2, $iCount = 0)
; Description:
; Parameters:
;    $sString_In   - The string to be stripped
;          $sChr   - The characters to be stripped (case sensitive)
;        $iFlags   - Flag to indicate the type of stripping that should be performed (add the flags together for multiple operations):
;                    1 = strip leading instances of characters in $sChr
;                    2 (Default) = strip trailing instances of characters in $sChr
;                    4 = Replace multiple instances of characters in $sChr with a single instance
;                    8 = strip all instances of $sChr (over-rides all other flags)
;        $iCount   - The max number of leading or trailing instances of $sChr to strip
;                    0 (Default) = Strip all
;
; Requirement:   None.
; Return Value:  Stripped string
; Author:       Bowmore
;
; Notes:
;
; Examples:
;         _StringStripChr("AAAAbdcaefgA", "A", 3, 0)
;         would return
;         "bdcaefg"
;
;         _StringStripChr("AAAAbdcaefgA", "A", 2, 0)
;         would return
;         "AAbdcaefg"
;
;         _StringStripChr("ABCAAbdcaefgA", "AB", 7, 0)
;         would return
;         "CAbdcaefg"
;
;         _StringStripChr("ABCAAbdcaefgA", "ABe", 8, 0)
;         would return
;         "Cbdcafg"
;
;========================================================================================================
Func _StringStripChr($sString_In, $sChr, $iFlags = 2, $iCount = 0)
    Local $sNewString = $sString_In
    Local $sChr1 = ""
 
    If (BitAND($iFlags, 8) = 8) Then
        For $i = 1 To StringLen($sChr)
            $sChr1 = StringMid($sChr, $i, 1)
            $sNewString = StringReplace($sNewString, $sChr1, "", 0, 1)
        Next
    Else
        If (BitAND($iFlags, 4) = 4) Then
            For $i = 1 To StringLen($sChr)
                $sChr1 = StringMid($sChr, $i, 1)
                $sNewString = StringRegExpReplace($sNewString, $sChr1 & "{2,}", $sChr1)
            Next
        EndIf
 
        If (BitAND($iFlags, 2) = 2) Then
            If $iCount = 0 Then
                While (StringInStr($sChr, StringRight($sNewString, 1), 1))
                    $sNewString = StringTrimRight($sNewString, 1)
                WEnd
            Else
                For $i = 1 To $iCount
                    If (StringInStr($sChr, StringRight($sNewString, 1), 1)) Then
                        $sNewString = StringTrimRight($sNewString, 1)
                    Else
                        ExitLoop
                    EndIf
                Next
            EndIf
        EndIf
 
        If (BitAND($iFlags, 1) = 1) Then
            If $iCount = 0 Then
                While (StringInStr($sChr, StringLeft($sNewString, 1), 1))
                    $sNewString = StringTrimLeft($sNewString, 1)
                WEnd
            Else
                For $i = 1 To $iCount
                    If (StringInStr($sChr, StringLeft($sNewString, 1), 1)) Then
                        $sNewString = StringTrimLeft($sNewString, 1)
                    Else
                        ExitLoop
                    EndIf
                Next
            EndIf
        EndIf
    EndIf
 
    Return $sNewString
 
EndFunc   ;==>_StringStripChr

EDIT: Corrected name of function used in examples.

Fixed option 4 to work as described.

Fixed case sensitivity issue with options 1 and 2

Edited by Bowmore

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

A very useful function!

Don't want to nit-pick, but: The examples use the wrong/old function name :graduated:

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

You need to choose : _StringStripChr or _StringTrimChr ! :graduated:


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

@water and wakillon

Thanks, it's a good job someone is paying attention as I obviously wasn't when I edited this before posting. I've corrected the examples in the original and corrected a couple of other bugs.


"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

That's nice now that you have fixed the examples. :graduated:

Edited by czardas

Share this post


Link to post
Share on other sites

A suggestion : add a Flag to indicate if the operations should be case sensitive or not. :graduated:


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Edit: After finding this function did not work, I searched some more and found StringReplace() which works just fine. So it really does not matter to me if this is fixed. Leaving it here FAI (For Anyone's Information).

I realize this function is kinda old, but I tried it to help me get rid of periods so I can compare versions. My code comparing with the periods keeps picking the 3.2.5.50 version instead of the 3.2.5.155 version, so I copied / pasted the entire StringStripChar function in the Include Directory.

The output in my script were periods. IE: '3.2.5.155' became '.' and '3.2.5.50' became '.'

I pulled out the _StringStripChar and made it simple. :thumbsup:

#include <MsgBoxConstants.au3>
#include <StringStripChr.au3>

Local $sMyDir1 = "3.2.5.50"
Local $sMyDir2 = "3.2.5.155"

MsgBox($MB_OK,"", "Before Stripping "&$sMyDir1&" and "&$sMyDir2)
$sMyDir1 = _StringStripChr($sMyDir1,".",4,0)
$sMyDir2 = _StringStripChr($sMyDir2,".",4,0)
MsgBox($MB_OK,"", "Stripped Periods become "&$sMyDir1&" and "&$sMyDir2)

I can't include my entire script due to confidentiality issues. :pirate:

I don't know what changed, but this does not work for me, or maybe it never did for periods. :

If this code gets fixed, I'll try it again. Meantime I guess I will have to do this the "Old Math" way...

Thanks,

JibsMan :bike:

Edited by JibsMan

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

  • Similar Content

    • JustinZandee
      By JustinZandee
      I need a random string generator which creates 15 letters/numbers.
      How can I make that?
    • TrashBoat
      By TrashBoat
      Is this possible:
      Executing a function from an include, but taking the function name from a gui input and then executing that function using the include:
      #include <Something.au3> ;input reads "Tree" $functionName = GuiCtrlRead($input1) $functionName(1) And the include is gonna have
      Func Tree($x) If $x = 1 Then $this = "text" MsgBox(0,$this,"whatever") EndFunc is it possible?
    • anthonyjr2
      By anthonyjr2
      Hi guys,
      I am pretty bad with regex, and am having some trouble trying to come up with an expression for a certain type of string. Basically I want to be able to tell if a string is of the format:
      AA#####A
      Where the A's are any letter from A-Z and the #'s are any digit from 0-9.
      I've been playing around with a regex tester online for a while but I can't really seem to grasp the concept very well. Could anyone give me any tips?
      This isn't exactly an AutoIt specific question which is why I didn't post it in General Help & Support.
    • LoneWolf_2106
      By LoneWolf_2106
      Hi everybody,
      i have a question related to strings items in an Array and sorting. Maybe someone can advice me how to solve the issue.
      I have an Array of strings, every item of the Array is as following:
      INFO [13.06.2017 11:48:01] [Thread-13] [ConGenImpUsb -> waitForConnection]  INFO [07.06.2017 08:55:44] [main] MDU5 - Ver 5.1x I want to sort the item in the array by date and time, is there any function which allows me to sort by date/time?
      Thanks in advance
    • hcI
      By hcI
      Hello, i'm making a little script that lets users enter a question and if the script can't answer to, it ask what would be the answer (a bit like siri on IPhones). And I try to display the "supposed array" I still have an error.. I searched on Google and got nothing looked in the help file too.. I'm here to know if someone have a solution to my error please.
      Here is where $a and $b should become arrays :
      Dim $dossier = @AppDataDir & "\Siri2" Dim $chemin = $dossier & "\data.ini" Global $a = _InfoRead($chemin, 0) Global $b = _InfoRead($chemin, 1) with this function :
      Func _InfoRead($path, $mode = 0) If DirGetSize($dossier) = -1 Then Return SetError(-4) If Not FileExists($chemin) Then Return SetError(-5) Local $readed = IniReadSection($path, "data") If @error Then Return SetError(-3) $size = $readed[0][0] Local $return[$size] For $i = 1 To $size Step 1 $return[$i - 1] = $readed[$i][$mode] Next Return $return EndFunc and the thing that i don't understand is that, after _InfoRead return affected to $a, I can't use _ArrayDisplay because of  error 1 "$aArray is not an array" (and same fpr the variable $b)..
      Can please someone help me to understand why is it doing this ?

      -hcI