Blog Details

String Function


                                
                                    <h2>* String Function :-</h2>
<h2>1) String Length</h2>
<ul>
	<li>The length of a string (a string object) is found in the built in property <strong>length</strong>:</li>
</ul>
<h3>Example :</h3>
&lt;html&gt;
&lt;body&gt;

&lt;script&gt;

var txt = "Hello World!";
document.write("&lt;p&gt;" + txt.length + "&lt;/p&gt;");

var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.write("&lt;p&gt;" + txt.length + "&lt;/p&gt;");

&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;
<h3><strong>Output :</strong></h3>
12

26

&nbsp;
<h2>2) Matching Content</h2>
<ul>
	<li><span style="line-height: 13px;">The <strong>match()</strong> method can be used to search for a matching content in a string:
</span></li>
</ul>
<h3>Example :</h3>
&lt;html&gt;
&lt;body&gt;

&lt;script&gt;

var str="Hello world!";
document.write(str.match("world") + "&lt;br&gt;");
document.write(str.match("World") + "&lt;br&gt;");
document.write(str.match("world!"));

&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;
<h3><strong>Output :</strong></h3>
world
null
world!

&nbsp;
<h2>3) Replacing Content</h2>
<ul>
	<li>The <strong>replace()</strong> method replaces a specified value with another value in a string.</li>
</ul>
<h3>Example :</h3>
str="Please visit Microsoft!"
var n=str.replace("Microsoft","W3Schools");

&nbsp;
<h2>4) Upper Case and Lower Case</h2>
<ul>
	<li>A string is converted to upper/lower case with the methods <strong>toUpperCase() </strong>/<strong> toLowerCase()</strong>:</li>
</ul>
<h3>Example :</h3>
var txt="Hello World!";       // String

var txt1=txt.toUpperCase();   // txt1 is txt converted to upper
var txt2=txt.toLowerCase();   // txt2 is txt converted to lower

&nbsp;
<h2>5) Convert a String to an Array</h2>
<ul>
	<li><em id="__mceDel">A string is converted to an array with the built in method string.<strong>split()</strong>:</em></li>
</ul>
<h3>Example :</h3>
<div>txt="a,b,c,d,e"   // String
txt.split(",");   // Split on commas
txt.split(" ");   // Split on spaces
txt.split("|");   // Split on pipe</div>
<div></div>
                                    


Categories

Javascript

Latest posts

13.05.14
Implementing CRUD Operations ¶

<ul> <li>Now comes the fun part. We would like t

13.05.14
View

<ol> <li><a href="http://www.yiiframework.com/do

13.05.14
Creating Model

<ol> <li><a href="http://www.yiiframework.com/do

Get Free Quote