this.Blog.Find(entry => entry.IsHelpful);
 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>

Kick it on DotNetKicks.com
Saturday, May 17, 2008 10:32:30 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]  Ajax | ASP.Net | Javascript