views:

7847

answers:

5

I'm trying to move some javascript code from MicrosoftAjax to JQuery. I use the javascript equivalents in MicrosoftAjax of the popular .net methods, e.g. String.format(), String.startsWith() .. etc, are there equivalents to them in JQuery?

Thanks

A: 

I haven't found any string functions in jquery but why not just use the Javascript string object?

For example:

var myStr = new String($("#selector").val());
ryanulit
Come on, can you at least comment when you down vote? Not only is it common courtesy, but it would help me out in learning why this is a bad idea.
ryanulit
Sure, I suppose I will comment. Microsoft's Ajax framework has helper functions that "add to" the Javascript framework (via the prototype object). Now he wants to use those same functions
Josh Stodola
(sorry my previous comment was submitted prematurely because this site sucks in IE) ...in jQuery. Basically, your answer is way out of context. Don't be offended!
Josh Stodola
Ah, got it, haha. I was obviously going the totally wrong way with that.
ryanulit
+1  A: 

I came across this while doing some Googling: jquery.strings.js. However, it looks like there may be a conflict with another jQuery plugin. User beware.

Alternatively, depending on the situation, you could stick with using .net methods to format your strings. For example, if you are doing an ajax request, you could format the result with your web method before returning to the client-side.

Acorn

Acorn
+23  A: 

The source code for ASP.NET AJAX is available for your reference, so you can pick through it and include the parts you want to continue using into a separate JS file. Or, you can port them to jQuery.

Here is the format function...

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;
}

And here are the endsWith and startsWith prototype functions...

String.prototype.endsWith = function (suffix) {
  return (this.substr(this.length - suffix.length) === suffix);
}

String.prototype.startsWith = function(prefix) {
  return (this.substr(0, prefix.length) === prefix);
}
Josh Stodola
Doesn't look like there's much to it. The JavaScript version doesn't have all the fancy number formatting stuff, obviously. http://blog.stevex.net/index.php/string-formatting-in-csharp/
Nosredna
I can see how they would be useful. Especially if you were used to having them.
Nosredna
Wow, I actually already thought about this but also thought it was not possible because of the license, didn't know they released it under Microsoft Permissive License, thanks a lot for this
Waleed Eissa
License or no license.. there's only one right way to write something so simple
Infinity
+3  A: 

There is an official option: jQuery.validator.format.

Comes with jQuery validation plug-in 1.6 (at least).
Quite similar to the String.Format found in .NET.

rsenna
+5  A: 

This is a faster (and prototypical) variation of the function that Josh posted:

String.prototype.format = function() {
    var s = this,
        i = arguments.length;

    while (i--) {
        s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
    }
    return s;
};

Usage:

'{0} {0} {1} {2}'.format(3.14, 'abc', 'foo'); // outputs: 3.14 3.14 abc foo
'Your balance is {0} USD'.format(77.7) 
Infinity