Emulate ASP.NET's AppendFormat Method In Javascript


/ Published in: JavaScript
Save to your folder(s)

Learn how to emulate ASP.NET's AppendFormat method in Javascript. AppendFormat is essentially a simple form of concatenation in ASP.NET.


Copy this code and paste it in your HTML
  1. <script type="text/javascript">
  2. function AppendFormat(input,arr)
  3. {
  4. for (i = 0; i < arr.length; i++)
  5. {
  6. while (input.indexOf("{"+i+"}") != -1)
  7. {
  8. input = input.replace("{"+i+"}", arr[i]);
  9. }
  10. }
  11. return input;
  12. }
  13.  
  14. document.write(AppendFormat("Lorem ipsum dolor sit {0} {1} amet, consectetur {0} adipiscing {0} elit.", ["TEST", "TEST2"]));
  15. </script>

URL: http://www.nealgrosskopf.com/tech/thread.asp?pid=54

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.