Jump to content

Strip any characters from a string function


Bowmore
 Share

Recommended Posts

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

Link to comment
Share on other sites

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 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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

Link to comment
Share on other sites

  • 3 years later...

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
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

×
×
  • Create New...