[JavaScript] All About JavaScript String Object Methods, Explained with Real Life Usages - Part 1.
Explore essential String Object methods such as indexOf(), lastIndexOf(), slice(), subString(), substr(), replace(), toUpperCase() and toLowerCase().
JavaScript String Object Methods That Will Be Covered In Part 1:
indexOf().
lastIndexOf().
slice().
subString().
substr().
replace().
toUpperCase().
toLowerCase().
Methods That Will Be Covered In Part 2:
Click here to check out Part 2.
concat().
trim().
padStart().
padEnd().
charAt().
charCodeAt().
startsWith().
endsWith().
1. indexOf()
This method finds the index number of the first element given in the parameter.
It receives one parameter with the String type.
If JavaScript cannot find the given String, it returns -1.
It is case-sensitive, so be aware of it.
let str = "JavaScript";
console.log(str.indexOf("S")); // 4
console.log(str.indexOf("o")); // -1
In real life, you can use indexOf() to validate a user's input.
let tel = "000-0000-0000";
if (tel.indexOf("-") > -1) {
console.log("Please enter your phone number without hyphens.");
}
2. lastIndexOf()
This method returns the last index number of the element given in the parameter.
It also allows you to set the starting index.
Let's just have a look at an example:
let str = "JavaScript";
console.log(str.lastIndexOf("a")); // 3
// starts searching from index 5.
console.log(str.indexOf("i", 5)); // 8
3. slice()
This method slices a specific part of your String. It receives one or two parameters.
When it receives two parameters
The first parameter is the start index.
The second parameter is the end index, but the string is sliced out without including the end index.
When it receives only one parameter, the first parameter is the start index. It will slice the String starting from the start index you set.
It receives a negative number, which means you are slicing in the reversed direction.
let str = "JavaScript";
console.log(str.slice(0, 4)); // Java (notice it does not include index 4)
console.log(str.slice(4)); // Script
console.log(str.slice(-6)); // Script
It can be used in this way:
let str = "JavaScript";
console.log(str.slice(str.indexOf("Script"))); // Script
4. subString()
This method is used to slice the string.
Almost the same as slice() except that it does not receive a negative number.
let str = "JavaScript";
console.log(str.subString(0, 4)); // Java
5. substr()
This method is useful when you know the length of the String you want to cut out.
It is because the method receives two parameters.
The start Index
The length of string you want to cut out.
let str = "JavaScript";
console.log(str.substr(4, 3)); // Prints Scr
6. replace()
Replace() method receives two parameters
String to be replaced
The new value
let str = "JavaScript";
console.log(replace("Java", "Type")); // TypeScript
7. toUpperCase()
As the method name tells you, it turns the String into uppercase.
let str = "JavaScript";
console.log(str.toUpperCase()) // JAVASCRIPT
8. toLowerCase()
It turns the String into lowercase.
let str = "ChatGPT";
console.log(str.toLowerCase()); // chatgpt