startsWith() method

The startsWith() string method in Apps Scripts is used to check if a string starts with a specified character or set of characters. It returns a boolean value of true if the string starts with the specified characters and false otherwise.

For example, you can use startsWith() to check if a URL starts with “https://” before allowing a user to proceed to that website. Another example is to check if a file name starts with a specific prefix before processing it further.

Example 1:

var country = "United States of America";
if (country.startsWith("United")) {
  Logger.log("Country name starts with United.");
} else {
  Logger.log("Country name does not start with United.");
}

In this example, the startsWith() method is used to check if the country name starts with “United”. Since the country name is “United States of America”, the condition is true and the log statement “Country name starts with United” is printed.

Example 2:

var city = "New York";
if (city.startsWith("Los")) {
  Logger.log("City name starts with Los.");
} else {
  Logger.log("City name does not start with Los.");
}

In this example, the startsWith() method is used to check if the city name starts with “Los”. Since the city name is “New York”, the condition is false and the log statement “City name does not start with Los” is printed.