28.05.2013 Views

javascript by example

javascript by example

javascript by example

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Capitolo 1<br />

String.prototype.Trim=function () {<br />

return this.replace(/\s+$|^\s+/g,"");<br />

}<br />

Così potremmo usare:<br />

var s = " test ";<br />

s.Trim(); // = "test"<br />

Linguaggio<br />

32 Con quale carattere inizia la stringa...<br />

Volete sapere al volo se una stringa inizia con un dato carattere?<br />

Estendiamo String:<br />

String.prototype.startsWith=function(s){<br />

}<br />

return (this.substring(0,s.length).toLowerCase()==s.toLowerCase());<br />

Quindi:<br />

var s="casa";<br />

var b = s.startsWith("c");//true<br />

33 ...e con quale finisce.<br />

Naturalmente è possibile anche implementare l'endsWith:<br />

String.prototype.endsWith=function(s){<br />

return this.substr(this.length - s.length).toLowerCase ()==s.toLower<br />

Case ();<br />

}<br />

Quindi:<br />

var s="casa";<br />

var b = s.startsWith("a");//true<br />

I libri di ioPROGRAMMO/Javascript <strong>by</strong> Example 39<br />

JAVASCRIPT<br />

BY EXAMPLE

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!