Loading, please wait...

Methods and Properties

Strings Methods and Properties

The string has many methods and properties which helps users to manipulate, search data in order to access data easily and efficiently.

 

String Properties

 

Property

Description

length()

Returns the length of a string

 

 

String Methods

 

Methods

Description

concat()

Returns a new string which is the union of two or more strings.

indexOf()

Returns the index value of a specified string.

search()

Returns the position of a specified string.

slice()

Extracts a section of a string.

toUpperCase()

Converts string to uppercase letters.

toLowerCase()

Converts string to lowercase letters.

split()

Split a string into an array of substrings.

replace()

Search a specified string and replace that with the new string.

charAt()

Returns the character by using the specified index value

 

 

The length Property

This property helps in calculating the length of a string by counting the number of characters.

 

Code:

<html> 
<head> 
</head> 
<body>
<h3> String Property </h3>
<h4> The length property helps in calculating the length of a string</h4>  
<script>
var str = "Welcome TutorialsLink";
document.write("Welcome TutorialsLink has "+str.length +" characters.");
</script>
</body> 
</html> 

 

 

Output:

 

 

 

 

The concat() Method

This method is used to get a new string which is a union of two or more strings.

 

Code:

<html> 
<head> 
</head> 
<body>
<h3> String Method </h3>
<h4> The concat() method helps in joining two or more strings</h4>  
<script>
var str = "Welcome";
var str1 = "TutorialsLink";
var string = str.concat(" "+str1);
document.write(string);
</script>
</body> 
</html> 

 

Output:

 

 

 

The indexOf() Method

This method returns the index value of a specified string.

 

Code:

<html> 
<head> 
</head> 
<body>
<h3> String Method </h3>
<h4> The indexOf() method returns the index value of a specified string.</h4>  
<script>
var str = "Welcome to the TutorialsLink";
var str1 = str.indexOf("to");
document.write(str + "<br><br>");
document.write("The index value of a string 'to' is "+str1);
</script>
</body> 
</html> 

 

Output:

 

 

 

 

The slice() Method

This method helps in extracting a part of a string and return that extracted part.

 

Code:

<html> 
<head> 
</head> 
<body>
<h3> String Method </h3>
<h4> The slice() method extracts a part of a string.</h4>  
<script>
var str = "Welcome to the TutorialsLink";
var str1 = str.slice(14,28);
document.write(str + "<br><br>");
document.write("The extracted part of a string is "+str1);
</script>
</body> 
</html> 

 

Output:

 

 

 

The replace() Method

This method replaces a part of a string with the specified value.

 

Code:

<html>  
<head> 
</head> 
<body>
<h3> String Method </h3>
<h4> The replace() method replaces a part of a string with the specified string.</h4>  
<script>
var str = "Welcome TutorialsLink";
var str1 = str.replace("Welcome","Hello");
document.write(str + "<br><br>");
document.write("After replacing, the string is "+str1);
</script>
</body> 
</html> 

 

Output:

 

 

 

 

The charAt() Method

This method returns the character by using the specified index value.

 

Code:

<html> 
<head> 
</head> 
<body>
<h3> String Method </h3>
<h4> The charAt() method returns the character by using the specified index value.</h4>  
<script>
var str = "Welcome TutorialsLink";
var str1 = str.charAt(8);
document.write(str + "<br><br>");
document.write("The character by using specified index is "+str1);
</script>
</body> 
</html> 

 

Output: