0% found this document useful (0 votes)
25 views

Javascript String Methods

The document discusses various string methods in JavaScript including length, indexOf, lastIndexOf, search, replace, toUpperCase, toLowerCase, and concat. It provides examples of each method and explains what they do.

Uploaded by

Malachi Lama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Javascript String Methods

The document discusses various string methods in JavaScript including length, indexOf, lastIndexOf, search, replace, toUpperCase, toLowerCase, and concat. It provides examples of each method and explains what they do.

Uploaded by

Malachi Lama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

WEEK 14 - Lecture 

JavaScript String Methods 


String Methods and Properties 
Primitive values, like "John Doe", cannot have properties or methods (because they
are not objects). 
But with JavaScript, methods and properties are also available to primitive values,
because JavaScript treats primitive values as objects when executing methods and
properties. 
 

String Length 
The length property returns the length of a string: 
<!DOCTYPE html> 
<html> 
<body> 
 <h2>JavaScript String Properties</h2> 
 <p>The length property returns the length of a string:</p> 
 <p id="demo"></p> 
 <script> 
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
var sln = txt.length; 
document.getElementById("demo").innerHTML = sln; 
</script> 
 </body> 
</html> 
 

Finding a String in a String 


The indexOf() method returns the index of (the position of) the first occurrence
of a specified text in a string: 
var str = "Please locate where 'locate' occurs!"; 
var pos = str.indexOf("locate"); 
 
JavaScript counts positions from zero. 
0 is the first position in a string, 1 is the second, 2 is the third ... 
 
The lastIndexOf() method returns the index of the last occurrence of a specified
text in a string: 
Example 
var str = "Please locate where 'locate' occurs!"; 
var pos = str.lastIndexOf("locate"); 
Both indexOf(), and lastIndexOf() return -1 if the text is not found. 
Example 
var str = "Please locate where 'locate' occurs!"; 
var pos = str.lastIndexOf("John"); 
 
 

Searching for a String in a String 


The search() method searches a string for a specified value and returns the
position of the match: 
var str = "Please locate where 'locate' occurs!"; 
var pos = str.search("locate"); 
 

Replacing String Content 


The replace() method replaces a specified value with another value in a string: 
str = "Please visit Microsoft!"; 
var n = str.replace("Microsoft", "W3Schools"); 
 
The replace() method does not change the string it is called on. It returns a new
string. 
 

Converting to Upper and Lower Case 


A string is converted to upper case with toUpperCase(): 
var text1 = "Hello World!";       // String 
var text2 = text1.toUpperCase();  // text2 is text1 converted to upper 
 

The concat() Method 
concat() joins two or more strings: 
var text1 = "Hello"; 
var text2 = "World"; 
var text3 = text1.concat(" ", text2); 
 
The concat() method can be used instead of the plus operator. These two lines do
the same: 
var text = "Hello" + " " + "World!"; 
var text = "Hello".concat(" ", "World!"); 
 
 

You might also like