Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 699663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:24:40+00:00 2026-05-14T03:24:40+00:00

This is driving me nuts. I believe I asked this exact same question, but

  • 0

This is driving me nuts. I believe I asked this exact same question, but I can’t find it any more (I used Stack Overflow search, Google Search, manually searched my posts, and searched my code).

I wanted something that would be like the C# String.Format where you could do something like

string format = String.Format("Hi {0}",name);

just for JavaScript of course and one person gave me a simple answer it was not like a jQuery plugin or anything, but I think you made some JSON thing or something, and it worked and was simple to use.

I for the life of me can’t find this post.

I do have this in my code, but I can’t seem to find anything that uses it and I am pretty sure I used it a couple of times:

String.prototype.format = function(o)
{
    return this.replace(/{([^{}]*)}/g,
       function(a, b)
       {
           var r = o[b];
           return typeof r === 'string' ? r : a;
       }
    );
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:24:41+00:00Added an answer on May 14, 2026 at 3:24 am

    Adapt the code from MsAjax string.

    Just remove all of the _validateParams code and you are most of the way to a full fledged .NET string class in JavaScript.

    Okay, I liberated the msajax string class, removing all the msajax dependencies. It Works great, just like the .NET string class, including trim functions, endsWith/startsWith, etc.

    P.S. – I left all of the Visual Studio JavaScript IntelliSense helpers and XmlDocs in place. They are innocuous if you don’t use Visual Studio, but you can remove them if you like.

    <script src="script/string.js" type="text/javascript"></script>
    <script type="text/javascript">
        var a = String.format("Hello {0}!", "world");
        alert(a);
    
    </script>
    

    String.js

    // String.js - liberated from MicrosoftAjax.js on 03/28/10 by Sky Sanders
    // permalink: http://stackoverflow.com/a/2534834/2343
    
    /*
        Copyright (c) 2009, CodePlex Foundation
        All rights reserved.
    
        Redistribution and use in source and binary forms, with or without modification, are permitted
        provided that the following conditions are met:
    
        *   Redistributions of source code must retain the above copyright notice, this list of conditions
            and the following disclaimer.
    
        *   Redistributions in binary form must reproduce the above copyright notice, this list of conditions
            and the following disclaimer in the documentation and/or other materials provided with the distribution.
    
        *   Neither the name of CodePlex Foundation nor the names of its contributors may be used to endorse or
            promote products derived from this software without specific prior written permission.
    
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED
        WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
        A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
        LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
        INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
        OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
        IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</textarea>
    */
    
    (function(window) {
    
        $type = String;
        $type.__typeName = 'String';
        $type.__class = true;
    
        $prototype = $type.prototype;
        $prototype.endsWith = function String$endsWith(suffix) {
            /// <summary>Determines whether the end of this instance matches the specified string.</summary>
            /// <param name="suffix" type="String">A string to compare to.</param>
            /// <returns type="Boolean">true if suffix matches the end of this instance; otherwise, false.</returns>
            return (this.substr(this.length - suffix.length) === suffix);
        }
    
        $prototype.startsWith = function String$startsWith(prefix) {
            /// <summary >Determines whether the beginning of this instance matches the specified string.</summary>
            /// <param name="prefix" type="String">The String to compare.</param>
            /// <returns type="Boolean">true if prefix matches the beginning of this string; otherwise, false.</returns>
            return (this.substr(0, prefix.length) === prefix);
        }
    
        $prototype.trim = function String$trim() {
            /// <summary >Removes all leading and trailing white-space characters from the current String object.</summary>
            /// <returns type="String">The string that remains after all white-space characters are removed from the start and end of the current String object.</returns>
            return this.replace(/^\s+|\s+$/g, '');
        }
    
        $prototype.trimEnd = function String$trimEnd() {
            /// <summary >Removes all trailing white spaces from the current String object.</summary>
            /// <returns type="String">The string that remains after all white-space characters are removed from the end of the current String object.</returns>
            return this.replace(/\s+$/, '');
        }
    
        $prototype.trimStart = function String$trimStart() {
            /// <summary >Removes all leading white spaces from the current String object.</summary>
            /// <returns type="String">The string that remains after all white-space characters are removed from the start of the current String object.</returns>
            return this.replace(/^\s+/, '');
        }
    
        $type.format = function String$format(format, args) {
            /// <summary>Replaces the format items in a specified String with the text equivalents of the values of   corresponding object instances. The invariant culture will be used to format dates and numbers.</summary>
            /// <param name="format" type="String">A format string.</param>
            /// <param name="args" parameterArray="true" mayBeNull="true">The objects to format.</param>
            /// <returns type="String">A copy of format in which the format items have been replaced by the   string equivalent of the corresponding instances of object arguments.</returns>
            return String._toFormattedString(false, arguments);
        }
    
        $type._toFormattedString = function String$_toFormattedString(useLocale, args) {
            var result = '';
            var format = args[0];
    
            for (var i = 0; ; ) {
                // Find the next opening or closing brace
                var open = format.indexOf('{', i);
                var close = format.indexOf('}', i);
                if ((open < 0) && (close < 0)) {
                    // Not found: copy the end of the string and break
                    result += format.slice(i);
                    break;
                }
                if ((close > 0) && ((close < open) || (open < 0))) {
    
                    if (format.charAt(close + 1) !== '}') {
                        throw new Error('format stringFormatBraceMismatch');
                    }
    
                    result += format.slice(i, close + 1);
                    i = close + 2;
                    continue;
                }
    
                // Copy the string before the brace
                result += format.slice(i, open);
                i = open + 1;
    
                // Check for double braces (which display as one and are not arguments)
                if (format.charAt(i) === '{') {
                    result += '{';
                    i++;
                    continue;
                }
    
                if (close < 0) throw new Error('format stringFormatBraceMismatch');
    
    
                // Find the closing brace
    
                // Get the string between the braces, and split it around the ':' (if any)
                var brace = format.substring(i, close);
                var colonIndex = brace.indexOf(':');
                var argNumber = parseInt((colonIndex < 0) ? brace : brace.substring(0, colonIndex), 10) + 1;
    
                if (isNaN(argNumber)) throw new Error('format stringFormatInvalid');
    
                var argFormat = (colonIndex < 0) ? '' : brace.substring(colonIndex + 1);
    
                var arg = args[argNumber];
                if (typeof (arg) === "undefined" || arg === null) {
                    arg = '';
                }
    
                // If it has a toFormattedString method, call it.  Otherwise, call toString()
                if (arg.toFormattedString) {
                    result += arg.toFormattedString(argFormat);
                }
                else if (useLocale && arg.localeFormat) {
                    result += arg.localeFormat(argFormat);
                }
                else if (arg.format) {
                    result += arg.format(argFormat);
                }
                else
                    result += arg.toString();
    
                i = close + 1;
            }
    
            return result;
        }
    
    })(window);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is driving me nuts, I'm sure it's rather simple but can't get it
I've seen this asked in several ways but can't find an answer that works.
This is driving me nuts, I've seen it before but can't replicate it or
This is driving me nuts. I can't seem to get the data template within
This is driving me nuts. I've looked at all the relevant MSDN tutorials but
Alright, this is driving me nuts because my regex is working on Rubular, but
OK, this is driving me nuts. I'm sure it is trivial, but I've been
This is driving me nuts, I can't see the problem. Using jQuery (I'm stuck
This is driving me nuts!@#!@# I can load the tinyMce plugin for jquery just
This is driving me nuts! I am getting the classic Can't connect to local

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.