Dear all,
I have a string like this .
<ak:Unit X="0" Y="1" Id="U33">
Do you know how to replace characters in double quotes of a string
For example
<ak:Unit X="0" Y="1" Id="U33"> ==> <ak:Unit X="Default1" Y="Default2" Id="Default3">
Sincerely
Substitute (or substitute into) is one option. Just remember to use escape character for double quote.
Names Default To Here(1);
str = "\[<ak:Unit X="0" Y="1" Id="U33">]\";
result = "\[<ak:Unit X="Default1" Y="Default2" Id="Default3">]\";
new_str = Substitute(str, "\!"0\!"", "\!"Default1\!"", "\!"1\!"", "\!"Default2\!"", "\!"U33\!"", "\!"Default3\!"");
result == new_str;
Given the specifics of the example string
<ak:Unit X="0" Y="1" Id="U33">
The actual substitute() function can be simplified to
Substitute(str, "0", "Default1", "1", "Default2", "U33", "Default3");
Got it ! thank you a lot
Hi Jthi,
It works!
Same question, but this time how could I substitute characters in a string by variable
For example:
Default1, Default2 and Default3 are now variables and their value would be changed
result = "\[<ak:Unit X="Default1" Y="Default2" Id="Default3">]\";
Here is an example where the first element is replaced from a variables value
Names Default To Here(1);
str = "\[<ak:Unit X="__one__" Y="1" Id="U33">]\";
sub1 = "theSub1";
mySub = Substitute(str,expr(__one__), sub1, "Default2", "U33", "Default3");
I think fairly rarely you would first add variable names to the string and then start replacing those with the values, but you could do it with Eval Insert.
Better option would be to directly add those values there. Example below has both options, first adds ¤Default1¤ and so on and replaces those by using Eval Insert Into (see scripting index), next one is using Associative array. Associative array is using the strings which are to be replaced as keys and values are the new values (I would use second option).
Names Default To Here(1);
str = "\[<ak:Unit X="0" Y="1" Id="U33">]\";
new_str = Substitute(str, "\!"0\!"", "\!"¤Default1¤\!"", "\!"1\!"", "\!"¤Default2¤\!"", "\!"U33\!"", "\!"¤Default3¤\!"");
Default1 = "test1";
Default2 = "test2";
Default3 = "test3";
Write("Eval Insert into: \!N");
Eval Insert Into(new_str, "¤");
Write(new_str);
Write("\!N\!NAssociative Array:\!N");
aa_replace = Associative Array();
aa_replace["\!"0\!""] = "\!"test1\!"";
aa_replace["\!"1\!""] = "\!"test2\!"";
aa_replace["\!"U33\!""] = "\!"test3\!"";
new_str2 = Substitute(str, aa_replace << get keys, aa_replace << get values);
write(new_str2);
If you know the starting tag (in this case, "ak:Unit"), then you can use Parse XML():
Names Default To Here( 1 );
ex = "\[<ak:Unit X="0" Y="1" Id="U33">]\";
Parse XML( ex,
On Element( "ak:Unit", Start Tag( xml = XML Attr() ) ),
);
xml << Remove( "" );
default 1 = "new X";
default 2 = "new Y";
default 3 = "new Id";
new vals = [=>];
new vals["X"] = default 1;
new vals["Y"] = default 2;
new vals["Id"] = default 3;
new string = "<ak:Unit";
keys = {"X", "Y", "Id"};
For( i = 1, i <= 3, i++,
new string ||= " " || keys[i] || "=\!"" || new vals[keys[i]] || "\!""
);
new string ||= ">";
Write( new string )