The syntax for this method is
str1.concat(str2) and its used to join the two strings
str2 and returns the concatenated string as the result.
concat() Method Example:
Below we have a simple code example to see the
concat() function in action:
This function just acts like the concatenation operator
+ which is also used to join two strings.
This method is used to convert a string into lowercase letters. It's a simple function, which operates on one string at a time and coverts all its alphabetical characters into lower case. Let's take an example,
let a = "WELCOME"; let b = a.toLowerCase(); console.log(b); // Logs: welcome
This method converts the string into uppercase letters and returns the result in the form of upper case. Let's take an example for this:
let x = "studytonight"; let y = x.toUpperCase(); console.log(y);
This method searches a string or a part of the string(sub string) in a given string value and returns the position of the first occurrence of the sub string to be searched.
The syntax for using the
search() function is
str1 is the string in which we want to search for
let str = "Please locate where locate occurs!"; console.log(str.search("locate")); // Logs: 7
slice() method is used to extract a part of a string(a substring) and it returns the extracted part as a new string. This method takes two parameters, the starting index position and ending index position(where the ending index is not included in the output).
If the parameter is negative in that case position is counted from the end of the String.
Let's take an example to understand:
let a = "Orange Banana Mango"; let subA = a.slice(7,14); //result of subA will be Banana console.log(subA)
In the example above, we have used the
slice() method to extract out the part of string starting from 7th index which is B character and upto the 14th index which means till the space after Banana.
Let's try with negative index now:
let a = "Orange Banana Mango"; let subA = a.slice(-7,19); //result of subA will be "a Mango" console.log(subA)
In the code example above, we have specified the first index as -7 so it will be counted from the end and the second index as positive so it will be counted from front. You can also use both negative values too.
It is used to split a string based on a given delimeter like space, hyphen etc and it returns an array with the broken parts of the string. Let's take an example to understand how it works:
let str = "x,y,z,u,v"; // this is a string let arr = str.split(","); //split based on commas console.log(arr); // Logs: x console.log(arr); // Logs: y
This method returns the character at a specified index (position) in a string. It takes index value as an argument.
Let's take an example and see:
let str = "Hello Studytonight"; let s = str.charAt(0); //returns H console.log(s);
trim() method removes extra spaces from the starting and ending of the string. Let's take an example to understand how it works.
In the above example we have a string with extra blank spaces in the front and at the end. So we used the
trim() method to remove them. This method should be used to validate and modify user input in forms to remove unnecessary white spaces added by the user in any form value.
replace() method replaces a specified part of the string(sub string or character) with another value in the given string. By default, this method only replaces the first occurrence of the substring which means if the substring to be replaced is present two times in the main string then only the first occurence will be replaced and also this method is case-sensitive.
Let's take an example to understand how it works:
let txt = "please visit google"; console.log(txt.replace("google", "StudyTonight")); let newTxt = "Try to Google 'Google'"; console.log(newTxt.replace("Google", "Bing"));
please visit StudyTonight
Try to Bing 'Google'
As you can see in the example above that only the first occurrence of substring "Google" is replaced while the second one is left as it is.
This method is similar to the
slice() method.The only difference is that the
substring() method does not accept a negative index. In this method we can extract out a part of the main string by providing the starting and the ending index. Let's take an example:
let str = "C,JAVA,PYTHON"; let res = str.substring(2,6); console.log(res) // Logs: JAVA
substr() method is similar to the
slice() method as it takes negative value too for index but there is one difference. The substr() method takes length of string to be extracted as the second parameter. For example, you can specify starting from index 2, pick the string of length 7, which means till the index 9.
Let's take an example to understand this:
let fruits = "Apple, Banana, Kiwi, Orange"; let substr = fruits.substr(6,6); console.log(substr);
|codePointAt()||The codePointAt() method returns a non-negative integer that is the Unicode code point value.|
|replace()||It searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced|
|search()||It searches a string for a specified value, or regular expression, and returns the position of the match|
|substring()||It extracts the characters from a string, beginning at a specified start position, and through the specified number of character|
In this tutorial we covered the important String methods which are most commonly required or used in development.