{

ES6 String Methods with examples in javascript| Learn Es6 tutorials


javascript ES6 String method examples

In this tutorial, We will have a look into EcmaScript 2015 String methods - startsWith() , endsWith(), includes(), repeat() method with example

ES6 String methods in javascript

Es6 introduced new methods of a String type 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()

Javascript ES6 String.prototype.startsWith() method example

startsWith() method search string matched from a given start index

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

  • String to search - substring to be searched
  • Index position - It is a position from which a search starts. By default is zero.

Example:

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

Javascript ES6 String.prototype.endsWith() method example

endsWith() method match another with from end index. It 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

  • Search string - string to search that matched from end
  • Index position - It is a position to match with the string, this is optional

Example

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

Javascript ES6 String.prototype.includes() method example

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

includes(searchstring,index)

It has two arguments

  • Search string - string to be matched with the given string
  • 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

Javascript ES6 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

Example:

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

Javascript ES6 Unicode methods

es6 introduced methods for dealing with Unicode strings. Here are the methods for String.fromCodePoint : Returns Unicode 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.