how to replace / insert character into double quote of a stringDear all,
I have a string like this .
Do you know how to replace characters in double quotes of a string
For example
0" Y="1" Id="U33"> ==> Default1" Y="Default2" Id="Default3">
Sincerely
Advanced Statistical ModelingRe: how to replace / insert character into double quote of a stringIf you know the starting tag (in this case, "ak:Unit"), then you can use Parse XML():
Names Default To Here( 1 );
ex = "\[<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 = "<UNIT>";
Write( new string )</UNIT></UNIT>
Re: how to replace / insert character into double quote of a stringI 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 = "\[<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);</UNIT>Re: how to replace / insert character into double quote of a stringHere is an example where the first element is replaced from a variables value
Names Default To Here(1);
str = "\[<UNIT x="__one__" y="1" id="U33">]\";
sub1 = "theSub1";
mySub = Substitute(str,expr(__one__), sub1, "Default2", "U33", "Default3");</UNIT>
Re: how to replace / insert character into double quote of a stringGot it ! thank you a lot
Re: how to replace / insert character into double quote of a stringHi 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 = "\[<UNIT x="Default1" y="Default2" id="Default3">]\";</UNIT>
Re: how to replace / insert character into double quote of a stringGiven the specifics of the example string
The actual substitute() function can be simplified to
Substitute(str, "0", "Default1", "1", "Default2", "U33", "Default3");
Re: how to replace / insert character into double quote of a stringSubstitute (or substitute into) is one option. Just remember to use escape character for double quote.
Names Default To Here(1);
str = "\[<UNIT x="0" y="1" id="U33">]\";
result = "\[<UNIT x="Default1" y="Default2" id="Default3">]\";
new_str = Substitute(str, "\!"0\!"", "\!"Default1\!"", "\!"1\!"", "\!"Default2\!"", "\!"U33\!"", "\!"Default3\!"");
result == new_str;</UNIT></UNIT>