First, let's declare a string with value "Hello, Welcome to Another Techs.":
let str = "Hello, Welcome to Another Techs.";
The string method toLowerCase () converts all characters in a given string to lowercase format. Similarly, the toUpperCase () method converts all characters to the uppercase format. These functions do not modify the original string.
Let's use a simple example to see these two methods:
console.log(str); console.log(str.toUpperCase()); console.log(str.toLowerCase());
When you run the above code in the console, you will receive the following output :
Hello, Welcome to Another Techs. HELLO, WELCOME TO ANOTHER TECHS. hello, welcome to another techs.
As the name suggests, the replace() method can help you replace the part of the string with another part. This method has two parameters: The first is the substring to be replaced, and the second is the substring to be replaced. This method does not modify the original string in any way.
The method trim() removes all spaces in the string, spaces before the first character and after the last character. This method does not require you to pass any parameters, nor does modify the original string. It is very useful for validating user input on forms.
Let's explore this string method with a new example:
let untrimmedString = " Welcome to Another Techs "; let trimmedString = untrimmedString.trim(); console.log(untrimmedString); console.log(trimmedString);
" Welcome to Another Techs " "Welcome to Another Techs"
concat() method is used to concatenate two or more strings. You can add one or more parameters to this method to concatenate them into a single string. It will not make any modifications to the original string. This is an example that shows concatenating two strings to form a new string:
let s2 = " How are you?"; let new_str = str.concat(s2); console.log(new_str);
Hello, Welcome to Another Techs. How are you?
charAt() string method returns the character at index specified in the string. It only accepts one parameter, the index of the character to be retrieved. The index value ranges from 0 to and the length is -1. This is an example of the charAt() method:
substring() method is used to obtain a substring or part of the original string. This method takes two parameters: start index and end index. The output substring starts from the specified start index and is printed to the end index -1.
This is a quick example of the substring () method:
The search () method helps to find a specific substring or characters in the original string. This method accepts a set of characters or substrings as parameters and keeps track of the entire string. When a match is found, the starting index of the matching part is returned. Otherwise, this method returns 1.
You can use the search() method in the following ways:
The indexOf () method can help you find the first index where the specified character or substring appears in . It starts from the left of and traces the string to check if the given parameter matches.
Let's take as an example to find the index where Another appears in the string:
If the given parameter does not exist in the string, the method returns with a value of 1.
Similarly, the lastIndexOf() method returns the index of the last occurrences of a given character or string. This is an example:
split() method is used to split all words or characters into a string according to the separator parameter passed to the method. The method's return type is an array. The array consists of characters or substrings, divided according to the given separator. The method does not modify the original string.
For example, if you pass a space ("") as the separator parameter to the divide method, the output will look like this:
let splitString = str.split(" "); console.log(splitString);
Array(5) [ "Hello,", "Welcome", "to", "Another", "Techs." ]
If you don't pass the parameter to the split() method, it will return a array containing a single element composed of the value of a string variable.
Similar to the charAt method, the charCodeAt() method returns ASCII valueof the character at the specified index. This string method only takes as a parameter, which is the index from which characters will be retrieved.
str.charCodeAt(9); str.charCodeAt(str.length - 1);
Similarly, the index value ranges from 0 to length-1. If you try to pass an index that exceeds the allowable limit, this method will return -1.