Shafayat Posted September 4, 2009 Posted September 4, 2009 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]
BartvanBeek Posted January 22, 2010 Posted January 22, 2010 (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 January 22, 2010 by BartvanBeek
dantay9 Posted January 22, 2010 Posted January 22, 2010 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
Mat Posted January 22, 2010 Posted January 22, 2010 And as short as it gets... MsgBox (0, "2", StringFormat("Name=%s, Function=%s, Age=%s", "David", "Salesperson", "48")) AutoIt Project Listing
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now