Click here to Skip to main content
15,353,547 members
Articles / Web Development / HTML
Article
Posted 20 May 2014

Stats

12K views
106 downloads
13 bookmarked

String Formatting in JavaScript

Rate me:
Please Sign up or sign in to vote.
4.57/5 (8 votes)
20 May 2014CPOL
String.Format creates strings from a pattern and values

Introduction

Replaces each format item 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

JavaScript
'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

JavaScript
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 into the string object. The parameter should be between the { } brackets.

JavaScript
"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
JavaScript
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 feel free to ask me if you would require any help for the same.

Your valuable feedback, comments, suggestions 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
Technical Lead 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. 2nd Best Mobile Article of January 2015
  2. 3rd Best Web Dev Article of May 2014
  3. 2nd Best Asp.Net article of MAY 2011
  4. 1st Best Asp.Net article of SEP 2010


Read More Articles...

Comments and Discussions

 
Questionsimple and easy to learn Pin
Jcmorin21-May-14 6:33
MemberJcmorin21-May-14 6:33 
from any .NET programmer this is super easy to learn and use.

Good job!
AnswerRe: simple and easy to learn Pin
Sunasara Imdadhusen21-May-14 18:29
professionalSunasara Imdadhusen21-May-14 18:29 

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

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