JavaScript equivalent to printf/String.Format

前端 未结 30 2491
囚心锁ツ
囚心锁ツ 2020-11-21 04:27

I\'m looking for a good JavaScript equivalent of the C/PHP printf() or for C#/Java programmers, String.Format() (IFormatProvider for .

相关标签:
30条回答
  • 2020-11-21 05:03

    There is "sprintf" for JavaScript which you can find at http://www.webtoolkit.info/javascript-sprintf.html.

    0 讨论(0)
  • 2020-11-21 05:04

    jsxt, Zippo

    This option fits better.

    String.prototype.format = function() {
        var formatted = this;
        for (var i = 0; i < arguments.length; i++) {
            var regexp = new RegExp('\\{'+i+'\\}', 'gi');
            formatted = formatted.replace(regexp, arguments[i]);
        }
        return formatted;
    };
    

    With this option I can replace strings like these:

    'The {0} is dead. Don\'t code {0}. Code {1} that is open source!'.format('ASP', 'PHP');
    

    With your code the second {0} wouldn't be replaced. ;)

    0 讨论(0)
  • 2020-11-21 05:05

    We can use a simple lightweight String.Format string operation library for Typescript.

    String.Format():

    var id = image.GetId()
    String.Format("image_{0}.jpg", id)
    output: "image_2db5da20-1c5d-4f1a-8fd4-b41e34c8c5b5.jpg";
    

    String Format for specifiers:

    var value = String.Format("{0:L}", "APPLE"); //output "apple"
    
    value = String.Format("{0:U}", "apple"); // output "APPLE"
    
    value = String.Format("{0:d}", "2017-01-23 00:00"); //output "23.01.2017"
    
    
    value = String.Format("{0:s}", "21.03.2017 22:15:01") //output "2017-03-21T22:15:01"
    
    value = String.Format("{0:n}", 1000000);
    //output "1.000.000"
    
    value = String.Format("{0:00}", 1);
    //output "01"
    

    String Format for Objects including specifiers:

    var fruit = new Fruit();
    fruit.type = "apple";
    fruit.color = "RED";
    fruit.shippingDate = new Date(2018, 1, 1);
    fruit.amount = 10000;
    
    String.Format("the {type:U} is {color:L} shipped on {shippingDate:s} with an amount of {amount:n}", fruit);
    // output: the APPLE is red shipped on 2018-01-01 with an amount of 10.000
    
    0 讨论(0)
  • 2020-11-21 05:05

    For use with jQuery.ajax() success functions. Pass only a single argument and string replace with the properties of that object as {propertyName}:

    String.prototype.format = function () {
        var formatted = this;
        for (var prop in arguments[0]) {
            var regexp = new RegExp('\\{' + prop + '\\}', 'gi');
            formatted = formatted.replace(regexp, arguments[0][prop]);
        }
        return formatted;
    };
    

    Example:

    var userInfo = ("Email: {Email} - Phone: {Phone}").format({ Email: "someone@somewhere.com", Phone: "123-123-1234" });
    
    0 讨论(0)
  • 2020-11-21 05:06

    I did not see the String.format variant:

    String.format = function (string) {
        var args = Array.prototype.slice.call(arguments, 1, arguments.length);
        return string.replace(/{(\d+)}/g, function (match, number) {
            return typeof args[number] != "undefined" ? args[number] : match;
        });
    };
    
    0 讨论(0)
  • 2020-11-21 05:08

    +1 Zippo with the exception that the function body needs to be as below or otherwise it appends the current string on every iteration:

    String.prototype.format = function() {
        var formatted = this;
        for (var arg in arguments) {
            formatted = formatted.replace("{" + arg + "}", arguments[arg]);
        }
        return formatted;
    };
    
    0 讨论(0)
提交回复
热议问题