Jump to content

StringSubstitute.au3


Shafayat
 Share

Recommended Posts

I made this for one of my recent projects. I hope it helps someone.

$g = StringSubstitute("a,b,c","2,3,6","c",",")
MsgBox(0,"",$g)
Func StringSubstitute($str1, $str2, $val, $chr1 = ",", $chr2 = ",")
    Local $test = StringSplit($str1,$chr1)
    If ($test[0] = 0) Then
    Else
        For $i = 1 to $test[0]
            If $test[$i] = $val Then
                Local $num = $i
                ExitLoop
            EndIf
        Next
        $test = 0
        $test = StringSplit($str2, $chr2)
        If $num > $test[0] Then
            SetError(-1)
            Return ""
        Else
            Return $test[$num]
        EndIf
    EndIf
EndFunc

[Not using this account any more. Using "iShafayet" instead]

Link to comment
Share on other sites

  • 4 months later...

Here a short version:

MsgBox(0,"",_StringSubstitute("Name=%1, Function=%2, Age=%3","David|Salesperson|48","|"))

Func _StringSubstitute($iTxt_String,$iTxt_Values,$iTxt_Seperator)
    Local $Arr_Values = StringSplit($iTxt_Values,$iTxt_Seperator)
    Local $i
    Do
        $i +=1
        IF StringInStr($iTxt_String,"%" & $i) > 0 Then
            $iTxt_String = StringReplace($iTxt_String,"%" & $i,$Arr_Values[$i])
        EndIf
    Until $i = $Arr_Values[0]
    Return $iTxt_String
EndFunc
Edited by BartvanBeek
Link to comment
Share on other sites

Going off of BartvanBeek, here is an even shorter version:

MsgBox(0, "", _StringSubstitute2("Name=%1, Function=%2, Age=%3", "David|Salesperson|48", "|"))

Func _StringSubstitute2($iTxt_String, $iTxt_Values, $iTxt_Seperator)
    Local $Arr_Values = StringSplit($iTxt_Values, $iTxt_Seperator)
    For $i = 1 To $Arr_Values[0]
        If StringInStr($iTxt_String, "%" & $i) > 0 Then $iTxt_String = StringReplace($iTxt_String, "%" & $i, $Arr_Values[$i])
    Next
    Return $iTxt_String
EndFunc
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...