JavaScript JS Tutorial JS Objects JS Functions JS Classes JS Async JS HTML DOM JS Browser BOM JS Web APIs JS AJAX JS JSON JS vs jQuery JS Graphics



JS String Methods

JavaScript is a powerful programming language that is widely used in web development. One of the most important data types in JavaScript is the string. A string is a sequence of characters that can be manipulated using various string methods. In this article, we will explore some of the most commonly used JS string methods.

Brief Explanation of JS String Methods

JS string methods are built-in functions that can be used to manipulate strings. These methods can be used to perform various operations on strings such as searching, replacing, splitting, and concatenating. Here are some of the most commonly used JS string methods:

  • charAt(): This method returns the character at a specified index in a string.
  • concat(): This method is used to concatenate two or more strings.
  • indexOf(): This method returns the index of the first occurrence of a specified value in a string.
  • lastIndexOf(): This method returns the index of the last occurrence of a specified value in a string.
  • replace(): This method is used to replace a specified value with another value in a string.
  • slice(): This method is used to extract a part of a string and returns the extracted part in a new string.
  • split(): This method is used to split a string into an array of substrings.
  • substr(): This method is used to extract a specified number of characters from a string, starting from a specified index.
  • toLowerCase(): This method is used to convert a string to lowercase.
  • toUpperCase(): This method is used to convert a string to uppercase.

Code Examples

Let's take a look at some code examples to see how these JS string methods work:

charAt()

The charAt() method returns the character at a specified index in a string:


var str = "Hello World!";
var char = str.charAt(0);
console.log(char); // Output: "H"

concat()

The concat() method is used to concatenate two or more strings:


var str1 = "Hello";
var str2 = "World";
var str3 = str1.concat(" ", str2);
console.log(str3); // Output: "Hello World"

indexOf()

The indexOf() method returns the index of the first occurrence of a specified value in a string:


var str = "Hello World!";
var index = str.indexOf("World");
console.log(index); // Output: 6

lastIndexOf()

The lastIndexOf() method returns the index of the last occurrence of a specified value in a string:


var str = "Hello World!";
var index = str.lastIndexOf("o");
console.log(index); // Output: 7

replace()

The replace() method is used to replace a specified value with another value in a string:


var str = "Hello World!";
var newStr = str.replace("World", "JavaScript");
console.log(newStr); // Output: "Hello JavaScript!"

slice()

The slice() method is used to extract a part of a string and returns the extracted part in a new string:


var str = "Hello World!";
var newStr = str.slice(0, 5);
console.log(newStr); // Output: "Hello"

split()

The split() method is used to split a string into an array of substrings:


var str = "Hello World!";
var arr = str.split(" ");
console.log(arr); // Output: ["Hello", "World!"]

substr()

The substr() method is used to extract a specified number of characters from a string, starting from a specified index:


var str = "Hello World!";
var newStr = str.substr(6, 5);
console.log(newStr); // Output: "World"

toLowerCase()

The toLowerCase() method is used to convert a string to lowercase:


var str = "Hello World!";
var newStr = str.toLowerCase();
console.log(newStr); // Output: "hello world!"

toUpperCase()

The toUpperCase() method is used to convert a string to uppercase:


var str = "Hello World!";
var newStr = str.toUpperCase();
console.log(newStr); // Output: "HELLO WORLD!"

Conclusion

JS string methods are powerful tools that can be used to manipulate strings in various ways. By using these methods, you can perform complex operations on strings with ease. Whether you are a beginner or an experienced developer, understanding these methods is essential for writing efficient and effective JavaScript code.

References

Activity