I had a situation where I had created a configurable variable value that expected a c# string.Format replacement structure. Then I was required to move the processing to the client. The replacement values wouldn't be available until the page had been served. So I created this function to port the functionality to javascript.
uses regular expressions (in javascript) and an variable length argument list. Now you can use your c# habits on the client...
function stringFormat(sFormat){ var sRet=""; var args = stringFormat.arguments; var sAry = sFormat.split(/{[0-9]}+/g); for(i=1;i<args.length;i++){ sRet+= sAry[i-1] + args[i]; } return sRet; }
The usage of this function is as follows:
var sArgument1 = "red";
var sArgument2 = "black";
var sResultString = stringFormat("I have a {0} car and a {1} pair of shoes.", sArgument1, sArgument2);
//the result will be
"I have a red car and a black pair of shoes."
Remember Me
Theme design by Jelle Druyts
Pick a theme: BlogXP calmBlue Candid Blue dasBlog Discreet Blog Blue Elegante essence Just Html Mono Movable Radio Blue Movable Radio Heat orangeCream Portal Project84 Slate Sound Waves Tricoleur
Powered by: newtelligence dasBlog 1.8.5223.2
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Rusty Zarse
E-mail