Click here to Skip to main content
Click here to Skip to main content

String formatting in JavaScript

, 20 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
String.Format creates strings from a pattern and values.

Introduction

Replaces each format items in a specified string with the text equivalent of a corresponding parameters value. One or more format items in a specified string with the string representation of a specified parameter value.

Syntax

'string with format{0}'.format(Object arg0, Object arg1, Object argn);
Parameters

{0} : type of string object

arg0 : The first object to format.

arg1 : The second object to format.

argn : The Nth object to format.

Using the code

String.prototype.format = function () {
    var args = arguments;
    return this.replace(/\{\{|\}\}|\{(\d+)\}/g, function (m, n) {
        if (m == "{{") { return "{"; }
        if (m == "}}") { return "}"; }
        return args[n];
    });
};

Example

This example shows the use of the string.Format method to combine three strings with formatting options. The format string itself is the first argument to the string.Format method and it is specified as a string literal.

The "{0}", "{1}", "{2}" and "{N}" indicate where the first, second, third and Nth arguments are inserted in to the string object. The parameter should be between the { } brackets.

"Hello {0}.{1}, Welcome to our new house at {2}.".format('Mr','Imdadhusen','Ahmedabad');

Output:

Hello Mr.Imdadhusen, Welcome to our new house at Ahmedabad.

More Examples

Input
var startDate = "21 APR 2014";
var endDate = "24 APR 2014";
"Your score is {0} out of {1}".format(175,250);
"Dear {0}, Your ticket is booked for {1} days from {2} to {3}. Thank you for booking from {4}".format('Imdadhusen',4,startDate, endDate, 'www.happybooking.com');
"World T{0} - {1}th match, Group {2}, {3} v {4}, {3} won by {5} wickets (with {6} balls remaining).  {4}'s next match will be on {7}.".format(20,13,2,'India','Pakistan', 7, 9, '25 Apr 2014');
Ouput
Your score is 175 out of 250 

Dear Imdadhusen, Your ticket is booked for 4 days from 21 APR 2014 to 24 APR 2014. Thank you for booking from www.happybooking.com

World T20 - 13th match, Group 2, India v Pakistan, India won by 7 wickets (with 9 balls remaining). Pakistan's next match will be on 25 Apr 2014.

Points of Interest

Using the above string utility you can pass n number of parameters with repetitive parameter to generate complete string.

Please fill free to ask me if you would require any help for the same.

Your valuable feedback, comment, suggestion are highly appreciated.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sunasara Imdadhusen
Software Developer (Senior) Infostretch Ahmedabad-Gujarat
India India
Aspiring for a challenging carrier wherein I can learn, grow, expand and share my existing knowledge in meaningful and coherent way.

sunaSaRa Imdadhusen
 
AWARDS:
  1. 1st Best Asp.Net article of SEP 2010
  2. 2nd Best Asp.Net article of MAY 2011
 
Read More Articles...
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
Questionsimple and easy to learn PinmemberJcmorin21-May-14 6:33 
AnswerRe: simple and easy to learn PinprofessionalSunasara Imdadhusen21-May-14 18:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141030.1 | Last Updated 20 May 2014
Article Copyright 2014 by Sunasara Imdadhusen
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid