Sign in to follow this  
Followers 0
Shafayat

StringSubstitute.au3

4 posts in this topic

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]

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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

Share this post


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

[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

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