C# String.format in JavaScript

As a mainly C# developer who has to work on JavaScript more and more, I do miss the string.format method that I do use very often (like when C++ guys misses the sprintf when they do C# Winking smile ).

I found someone re-implement string.format in JavaScript here.

String.format = function () {
	var s = arguments[0];
	for (var i = 0; i < arguments.length - 1; i++) {
		var reg = new RegExp("\\{" + i + "\\}", "gm");
		s = s.replace(reg, arguments[i + 1]);
	}
	return s;
}

You can find my jsfiddle exmple here. Enjoy coding!

Advertisements

One thought on “C# String.format in JavaScript

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s