ES6 New String Methods
Last Updated :
03 Jan, 2023
In ES6, four new methods were added to String. These methods are like a boon for programmers when it comes to string manipulation in JavaScript. In day to day programming, we often deal with strings. The first three methods also reduce the dependency on Regular Expression RegExp for certain tasks. Four ES6 New String Methods are described below:
- startsWith( queryString, position ) Method: This method returns either true, if the string starts with the provided string from the specified position, otherwise false. This method is case-sensitive. This method accepts two arguments :
- queryString: The string which has to be searched at the beginning of the String.
- position (optional): The position from where the search has to be start. Please note that its default value is 0.
Below example illustrates the first method of ES6 New String Methods (startsWith(queryString, position)).
Example:
< script >
let str = "GeeksforGeeks" ;
console.log(str.startsWith( "Geeks" ));
console.log(str.startsWith( "for" , 5));
console.log(str.startsWith( "geeks" ));
< /script>
|
Output:
true
true
false
- endsWith( queryString, length ) Method: This method returns either true, if the string ends with the provided string for specified length, otherwise false. This method is case-sensitive. This method accepts two arguments:
- queryString: The string which has to be searched at the end of the String.
- length (optional): The length of the string. Please note that its default value is length of the string.
Below example illustrates the second method of ES6 New String Methods (endsWith(queryString, length)).
Example:
<script>
let str = "GeeksforGeeks" ;
console.log(str.endsWith( "Geeks" ));
console.log(str.endsWith( "for" , 8));
console.log(str.endsWith( "geeks" ));
</script>
|
Output:
true
true
false
- includes( queryString, position ) Method: This method returns either true if the string is present in the provided string, otherwise returns false. This method is case-sensitive. This method accepts two arguments:
- queryString: The string which is to be searched in the String.
- position (optional): The position from where the search has to be start. Please note that its default value is 0.
Below example illustrates the third method of ES6 New String Methods (includes(queryString, position)).
Example:
<script>
let str = "GeeksforGeeks" ;
console.log(str.includes( "eks" ));
console.log(str.includes( "for" , 8));
console.log(str.includes( "geeks" ));
</script>
|
Output:
true
false
false
- repeat( count ) Method: This method accepts single argument which is an integer value that represents the number of times the string is to be repeated. This method returns the newly created string with ‘count’ times repeated old string.
Note: The argument provided i.e. count must be an positive integer.
Below example illustrates the fourth method of ES6 New String Methods (repeat(count)).
Example:
<script>
let str = "GeeksforGeeks" ;
console.log(str.repeat(2));
let newStr = str.repeat(3);
console.log(newStr);
</script>
|
Output:
GeeksforGeeksGeeksforGeeks
GeeksforGeeksGeeksforGeeksGeeksforGeeks
Template Literals: These literals allowed embedded expression. Single or double quotes are not it’s behavior rather that it used back-ticks “`”. There are two types of Template Literals.
String.raw() Method: The String.raw() method is used to print the backslash as it is and it does not bring the new line into the console.
Example:
<script>
var geeks = String.raw `A Online Computer \nScience Portal for Geeks`
console.log(geeks)
</script>
|
Output:
A Online Computer \nScience Portal for Geek
String.fromCodePoint() Method: The String.fromCodePoint() is an inbuilt function in JavaScript which is used to return a string or an element for the given sequence of code point value (ASCII value).
Example:
<script>
a = String.fromCodePoint(42);
b = String.fromCodePoint(66, 65, 102);
console.log(a + "<br>" )
console.log(b)
</script>
|
Output:
*
BAf
Share your thoughts in the comments
Please Login to comment...