Forum Moderators: open
substitute("Hello {0}, you are {1}.", "Webmaster World","great")
Which would result in:
Hello Webmaster World, you are great.
But maybe something more like sprintf is what I want? Anyone have anything like this?
<script type="text/javascript">
<!-- Hide this code from non-JavaScript browsers
function Substitute() {
var args=Substitute.arguments;
var Base=args[0];
var Seek,Len,ix1,ix2,ix3;
for (ix1=1; ix1<args.length; ix1++) {
ix2=ix1-1;
Seek='{'+ix2+'}';
if ((ix3=Base.indexOf(Seek)) > -1) {
Len=Seek.length;
Base=Base.substring(0,ix3)+args[ix1]+Base.substring(ix3+Len);
}
}
return Base;
} // Substitute
// End hiding -->
</script> <script type="text/javascript">
<!-- Hide this code from non-JavaScript browsers
a=prompt('A','');
b=prompt('B','');
c="Hello {0}, you are {1}.";
d=Substitute(c,a,b);
alert(d);
// End hiding -->
</script>
YAHOO.Tools.printf = function() {
var num = arguments.length;
var oStr = arguments[0];
for (var i = 1; i < num; i++) {
var pattern = "\\{" + (i-1) + "\\}";
var re = new RegExp(pattern, "g");
oStr = oStr.replace(re, arguments[i]);
}
return oStr;
}
With this, you would call:
YAHOO.Tools.printf("Showing {0} of {1}.", "1", "2");
And that would return:
Showing 1 of 2.