 Saturday, May 17, 2008
String extension methods in ASP.Net Ajax
In an earlier post, I showed you how you could add a trim() method to the String class in Javascript. Well, as I found out late last week, if you are using the ASP.Net Ajax framework, they have already done this work for you (as well as a few more methods). Here are the extension methods for the String class that are provided to you:
| Method |
Description |
| endsWith() |
Test to see if the string ends with a particular string |
| format() |
Similar to .Net's String.Format(), it replaces a set of tokens with some replacement values |
| localeFormat() |
Similar to format(), but it uses the current culture to format dates and numbers |
| startsWith() |
Test to see if the string begins with a particular string |
| trim() |
Strips both leading and trailing whitespace from the string |
| trimEnd() |
Strips whitespace from the end of the string |
| trimStart() |
Strips leading whitespace from the string |
One other nice thing it provides is a StringBuilder class similar to .Net's System.Text.StringBuilder class. This is especially nice when creating big chunks of HTML to throw in an innerHTML property. <input type="button" name="btn1" value="Click Me!" onclick="javascript:changeText();"><br /> <span id="myText"></span> <script type="text/javascript">
function changeText() { var sb = new Sys.StringBuilder(); // thank you, ASP.Net Ajax! sb.append('<h1>How cool is this?</h1>'); sb.append('<br />'); sb.append('You just clicked my button. '); sb.append('Now we will change some text');
$get('myText').innerHTML = sb.toString(); }
</script>
Saturday, May 17, 2008 10:32:30 PM (Eastern Standard Time, UTC-05:00) Ajax | ASP.Net | Javascript
 Tuesday, May 13, 2008
Implementing String.trim() in Javascript
Curiously, the String class in javascript does not natively expose a trim() method. Not a problem! Since it is a dynamic language, javascript allows you the ability to crack open a class and add new functionality to it. <script language="javascript" type="text/javascript">
String.prototype.trim = function() { return this.replace(/^\s+/,"").replace(/\s+$/,""); }
var s = new String(" Test me! ").trim();
alert("'" + s + "'");
</script>
The prototype keyword indicates that you are adding functionality to an existing class. In this case, we are adding a method called trim() to the String class that uses regular expressions to find any spaces (\s+ = one or more whitespace characters) at the beginning (^ = match at the START of a line) and then the end ($ = match at the END of a line) of the string, and replace the spaces we find with an empty string ("").
Tuesday, May 13, 2008 11:07:44 AM (Eastern Standard Time, UTC-05:00) Javascript
|

Subscribe to this feed
 Email Me
 Follow Me On Twitter
| | Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|---|
| 30 | 1 | 2 | 3 | 4 | 5 | 6 | | 7 | 8 | 9 | 10 | 11 | 12 | 13 | | 14 | 15 | 16 | 17 | 18 | 19 | 20 | | 21 | 22 | 23 | 24 | 25 | 26 | 27 | | 28 | 29 | 30 | 31 | 1 | 2 | 3 | | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
Search
Navigation
Tag Cloud
Archive
Blogroll
|