Jump to content

_StringStripChr()


James
 Share

Recommended Posts

Hi,

Another snippet I had from over my AutoIting. I know StringReplace() can do the same thing, but it's always nice to see people using their intuition isn't it :D

ConsoleWrite(_StringStripChr("AutoIt3 for the win!", "o") & @CRLF)

Func _StringStripChr($from, $what)
    $StripOut = $From
    For $i = 1 To StringLen($what)
        $StripOut = StringReplace($StripOut, StringMid($what, $i, 1), "")
    Next
    Return $StripOut
EndFunc

James :D

Link to comment
Share on other sites

Hi,

Another snippet I had from over my AutoIting. I know StringReplace() can do the same thing, but it's always nice to see people using their intuition isn't it :D

ConsoleWrite(_StringStripChr("AutoIt3 for the win!", "o") & @CRLF)

Func _StringStripChr($from, $what)
    $StripOut = $From
    For $i = 1 To StringLen($what)
        $StripOut = StringReplace($StripOut, StringMid($what, $i, 1), "")
    Next
    Return $StripOut
EndFunc

James :)

That is really awful :D

A replacement for StringReplace that uses StringReplace :D and even then doesn't work.

_StringStrip("anything you like at all because it makes no differenc","the quick brown fox jumps over the lazy dog") returns not a lot I would expect.

$stripout = $from seems redundant; why not just use $from?

But apart from that it's great. :D

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

This what i am expecting from such function:

$String = ", ,C:Some dirSome File,,"

$Strip_Results = _StringStripChars($String, ", ", 3, 0, 1) ;3 = Strip from the both sides
$Strip_Results = StringFormat("$Strip_Results = %sn@extended = %d", $Strip_Results, @extended)

ConsoleWrite($Strip_Results)

;===============================================================================
; Function Name:         _StringStripChars()
; Description:           Strip (replace) certain character(s) from the String.
;
; Parameter(s):          $sString - String that character(s) will be striped from.
;                        $sSubString - The character(s) to strip.
;                        $iFlag [Optional] - Defines the behaviour of stripping process (see Returned value(s)).
;                        $iCount [Optional] - Defines how many times to perform the strip (see Returned value(s)).
;                        $iGroupChars [Optional] - If this parameter is 1 (default is 0), then all characters in $sSubString grouped and replaced seperately.
;
; Requirement(s):        AutoIt 3.2.8.1 +
;
; Return Value(s):       On seccess - Return new string with stripped characters accourding to given $Flag and $iCount:
;                               $iFlag = 0 (default) replace the character(s) in $sString whetewer it founded - with this flag,
;                                   also @extended is set to number of $sSubString replaces in $sString.
;                               $iFlag = 1 replace the character(s) from the Left side of $sString.
;                               $iFlag = 2 replace the character(s) from the Right side of $sString.
;                               $iFlag = 3 replace the character(s) from the Bouth sides of $sString.
;
;                               $iCount = 0 replace all $sSubString char(s).
;                               $iCount > 0 replace that much $sSubString char(s).
;
;                        On failure - If lenght of given string is equel 0, then @error set to 1 and returned initial $sString.
;                       
; Author(s):             amel27, mod. by G.Sandler a.k.a MsCreatoR
;===============================================================================
Func _StringStripChars($sString, $sSubString, $iFlag = 0, $iCount = 0, $iGroupChars = 0)
    If StringLen($sString) = 0 Then Return SetError(1, 0, $sString)
    
    Local $sGroupChar_a = '(', $sGroupChar_b = ')'
    If $iCount < 0 Then Local $sGroupChar_a = '[', $sGroupChar_b = ']'
    
    $sSubString = StringRegExpReplace($sSubString, '([][{}()|.?+*^$])', '1')
    
    If $iGroupChars = 1 Then $sSubString = '[' & $sSubString & ']'
    
    Local $sPattern = '(?i)' & $sGroupChar_a & $sSubString & $sGroupChar_b
    Local $sPattern_Count = '{1,' & $iCount & '}'
    
    If $iCount <= 0 Then $sPattern_Count = '+'
    If $iFlag <> 0 Then $iCount = 0
    If $iFlag = 1 Then $sPattern = '(?i)^' & $sGroupChar_a & $sSubString & $sGroupChar_b & $sPattern_Count
    If $iFlag = 2 Then $sPattern = '(?i)' & $sGroupChar_a & $sSubString & $sGroupChar_b & $sPattern_Count & '$'
    If $iFlag = 3 Then $sPattern = '(?i)^' & $sGroupChar_a & $sSubString & $sGroupChar_b & $sPattern_Count & '|' & _
        $sGroupChar_a & $sSubString & $sGroupChar_b & $sPattern_Count & '$'
    
    $sString = StringRegExpReplace($sString, $sPattern, '', $iCount)
    Return SetExtended(@extended, $sString)
EndFunc

:D

Edited by MrCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Well I do sort of suck at AutoIt!

Not at all, that's not true. If I thought that I wouldn't have been so hard on you. :D
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Not at all, that's not true. If I thought that I wouldn't have been so hard on you. :D

Thanks Martin! :D

Posted Image

Yep, You Really Suck at Autoit :)

Well, that was a lucky thing I guess, I mean it was being compared to work by others who have so much more experience - Disk Manager, one of the best pieces of work I have done in AutoIt, I'm still thinking of ideas and running test ideas to improve it. One idea is to be able to expand the GUI so that there is a listview with a selected partition/drives contents - Difficult!

@Creator, that is a really good way of doing it, but I don't understand StringRegExp(), never have, anything that can help me?

Thanks all!

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...