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

    • cheeroke
      By cheeroke
      Hi all,
      I got this code and would like to be able to change Baud Rate and instead of sending character by character i would like to be able (if possible) to send whole string. But i don't know how to change it.
      I am taking input from file and processing whole line (this is done in FilesHandling.au3).
      To execute this i am just calling SendData("FileName", int) in "main" script.
      Any help very appreciated.
      #include <WinAPI.au3> #include <Array.au3> #include "FilesHandling.au3" ;init DLL function, we need handle to call the function $h = DllCall("Kernel32.dll", "hwnd", "CreateFile", "str", "\\.\COM19", "int", BitOR($GENERIC_READ,$GENERIC_WRITE), "int", 0, "ptr", 0, "int", $OPEN_EXISTING, "int", $FILE_ATTRIBUTE_NORMAL, "int", 0) $handle=$h[0] Func SendData($FileName, $LineNumber) ;string to be send $c = readFile($FileName, $LineNumber) $cLenght = StringLen($c) $aArray = StringSplit($c, "") ;_ArrayDisplay($aArray, "", Default, 64) For $i = 1 To $cLenght writeChar($handle, $aArray[$i], $cLenght) Next ;move to next line writeChar($handle, @CR,1) EndFunc ;write a single char func writeChar($handle,$c,) $stString = DLLStructCreate("char str") $lpNumberOfBytesWritten = 0 DllStructSetData($stString, 1, $c) $res = _WinAPI_WriteFile($handle, DllStructGetPtr($stString, "str"), 1,$lpNumberOfBytesWritten) if ($res<>true) then ConsoleWrite ( _WinAPI_GetLastErrorMessage() & @LF) EndIf EndFunc  
    • FroVN
      By FroVN
      Hi, i have a problem :" can't set the name of file with a special character like: \;/;";|;...  have anyway to short the StringInSrt and Stringreplace? i am using this code but too long
      $title=InputBox(0,'','','')
         if StringInStr($title,'\') or StringInStr($title,'/') or StringInStr($title,':') or StringInStr($title,'*') or StringInStr($title,'?') or StringInStr($title,'"') or StringInStr($title,'<') or StringInStr($title,'>') or StringInStr($title,'|') Then
             $title=StringReplace($title,'\','-')
              $title=StringReplace($title,'/','-')
               $title=StringReplace($title,':','-')
                $title=StringReplace($title,'*','-')
                 $title=StringReplace($title,'?','-')
                  $title=StringReplace($title,'"','-')
                   $title=StringReplace($title,'<','-')
                    $title=StringReplace($title,'>','-')
                     $title=StringReplace($title,'|','-')
         EndIf
       
    • mati
      By mati
      Hi,
      I wonder if
      AutoIt3Wrapper_run_debug_mode = Y can include run information about functions which are called within the main code. So far I noticed in the console that the function itself is called but no specific run information from  inside the function is provided. Is there a chance to show the entire debug log including functions in the console?
      I furthermore noticed a stop of information when a loop is entered. What could be the reason for that?
       
      Thanks for help.
    • 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?