endsWith() method

The endsWith() method in Apps Script is used to determine whether a string ends with the characters of another string. It returns a boolean value true if the calling string ends with the characters of the specified string, otherwise false.

Example 1: Checking If a String Ends with a Specified String
Suppose we have a string country containing the name of a country, and we want to check if it ends with the string “land”. We can use the endsWith() method like so:

var country = "Finland";
var endsWithLand = country.endsWith("land");
Logger.log(endsWithLand);

In this example, we define a string country containing the name of a country, and use the endsWith() method to check if it ends with the string “land”. The method returns a value of false, which is logged to the console.

Example 2: Checking If Multiple Strings End with a Specified String
Suppose we have an array cities containing the names of some cities, and we want to check if each one ends with the string “ville”. We can use the endsWith() method in a loop like so:

var cities = ["Nashville", "Jacksonville", "Louisville"];
for (var i = 0; i < cities.length; i++) {
  var endsWithVille = cities[i].endsWith("ville");
  Logger.log(endsWithVille);
}

In this example, we define an array cities containing the names of some cities. We use a for loop to iterate over the array, and use the endsWith() method to check if each city name ends with the string “ville”. The method returns a value of true for “Nashville” and “Louisville”, and false for “Jacksonville”, which are logged to the console.

By using the endsWith() method in Apps Script, we can easily check whether a string ends with a specified string, and use the resulting boolean value to perform various operations on strings.