{

ES6 features - New String methods| Learn Es6 tutorials


ES6 String methods

Es6 introduced new methods of a String object. In this blog post, I will cover the new string methods with examples.

Newly introduced String methods can be generic and Unicode methods.

Generic methods

  • startsWith()
  • endsWith()
  • includes()
  • repeat()

String.prototype.startsWith() method example

startsWith() method returns

true - if the passed string with the search string is matched from the starting index position, If the index position is specified, It checks at the nth position

false - search string is not matched.

Syntax

startsWith(searchstring,index)

The two arguments are 1. String to search - substring to be searched 2. Index position - It is a position from which a search starts. By default is zero.

const message = "This is a new string methods";
console.log(message.startsWith("This is")); // true
console.log(message.startsWith("Thias is")); // false
console.log(message.startsWith("is", 4)); // false
console.log(message.startsWith("is", 5)); // true

String.prototype.endsWith() method example

endsWith() method also takes two arguments

This method returns true if the string end is matched with another string.

Syntax

endsWith(searchstring,index)

It has two arguments

  1. Search string - string to search that matched from end 2. Index position - It is a position to match with the string, this is optional
const message = "This is a new string methods";
console.log(message.endsWith("methods")); // true
console.log(message.endsWith("methodsa is")); // false
console.log(message.endsWith("is", 4)); // true
console.log(message.endsWith("is", 5)); // false

String.prototype.includes() method example

This return is true if the search substring contains a string.

includes(searchstring,index)

It has two arguments

  1. Search string - string to be matched with the given string 2. Index position - It is starting position to begin searching the string, This is optional.
const message = "this is testing include";

console.log(message.includes("tesing")); // true
console.log(message.includes("adfadf")); // false
console.log(message.includes("is", 8)); // false
console.log(message.includes("is", 2)); // true

String.prototype.repeat() method example

This method repeats the given string a given number of times. It gives the output concatenate strings.

str.repeat(count)

parameter - the number of times to repeat the string

const message = "cloud";
console.log(message.repeat(2)); // cloudcloud
console.log(message.repeat(3)); // cloudcloudcloud

Unicode methods

es6 introduced methods for dealing with Unicode strings. Here are the methods for String.fromCodePoint String.prototype.codePointAt; String.prototype.normalize;

Conclusion

Learn new string methods examples startsWith() endsWith() includes() repeat() method.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.