Jump to content

Recommended Posts

Posted (edited)

Hi All,

     I have written a UDF for one of my requirement which replaces a single character in string with a sub string/another character. 

     I am using this for my requirement by calling below function as StrReplace("C:\Software\Autoit\Substr","\","\\") and gives result as C:\\Software\\Autoit\\Substr

     Please let me know if this can be improvised or any mistakes to correct.   

;===============================================================================
;
; Function Name:    StrReplace($INPUT_STRING)
; Description:      This function is to replace a character with another in a string.
; Parameter(s):     $INPUT_STRING     - Original String Value
;                    $STR_2_FIND     - Single Character to find the $INPUT_STRING
;                    $STR_2_REPLACE     - Substring/Multiple Characters to replace in place of     $STR_2_FIND value
; Requirement(s):   Replacing one single Character in a string with multiple Characters
; Return Value(s):  success - Output string after replacing a character with required character
;                    failure - 0
; Author(s):        smartkey
;
;===============================================================================

Func StrReplace($INPUT_STRING, $STR_2_FIND, $STR_2_REPLACE)
    Local $OUTPUT_STRING = ""
    If StringLen($INPUT_STRING) > 0 Then
        If StringMid($INPUT_STRING,1,1) = $STR_2_FIND Then
            $OUTPUT_STRING = $OUTPUT_STRING & $STR_2_REPLACE
        Else
            $OUTPUT_STRING = StringMid($INPUT_STRING,1,1)
        EndIf

        For $i=2 to StringLen($INPUT_STRING)
            If StringMid($INPUT_STRING,$i,1) = $STR_2_FIND Then
                $OUTPUT_STRING= $OUTPUT_STRING & $STR_2_REPLACE
            Else
                $OUTPUT_STRING= $OUTPUT_STRING & StringMid($INPUT_STRING,$i,1)
            EndIf
        Next
        Return $OUTPUT_STRING
    Else
        Return 0
    EndIf
EndFunc

Edited by smartkey
  • Moderators
Posted

@smartkey AutoIt comes with a StringReplace function, to which yours seems very similar. What would this provide that the original does not, except two fewer parameters?

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

  • 2 weeks later...
Posted

@JLogan3o13 before writing this function i have used StringReplace for replace \ with \\, but I could not get the output in right manner and now could not recall exact error. So I have tried in my own function, thought of sharing it for Single Character replacement(but not for multiple characters replacement). 

But after looking at your post, i tried again with StringReplace and it looks to be working for this also. I am not remembering what advantage the above function brought to me over StringReplace. 

Now I will change in my program with StringReplace and continue to see the challenges, if i come across any challenge, I will share in the same thread. Till then let me admit above function does not fetch any more than StringReplace. 

Thanks for the question about advantage and my apologies for wasting your time.

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
×
×
  • Create New...