.NET Style String Formatting with jQuery – Javascript Function to Support Token Replacement within a String
Here is a quick jQuery Plugin Announcement:
String concatenation and formatting are common aspects of almost every development effort. However, the combination of single and double quotes, along with escape characters, text, and plus signs (+) can be a bit overwhelming and difficult to remember. The .NET Framework provides a much cleaner approach with the String class’s Format method. It allows you to create string values by specifying insertion points for other values as well as special formatting.
String.Format(“Hello {0}”, “world”);
The string formatting approach described above can now be used with Javascript using a jQuery Plugin: .NET STYLE STRING FORMATTING – a simple and clever function to support token replacement within a string.
Given a string like “Hello, {0}” and calling the format function with a single argument like “world”.
var result = $.format(“Hello, {0}”, “world”);
The result variable will be set to “Hello, world”.
The function will support an arbitrary number of arguments including none. When a token is unused in the string it will be ignored and too many arguments will be ignored.
More details can be found here.
Related Resources:
Related posts:
