The toUpperCase() method in Apps Script is used to convert a given string to all uppercase letters. This method does not modify the original string but instead creates a new string that contains the uppercase version of the original string. This method can be useful when you need to standardize the case of strings in your code or when you need to compare strings in a case-insensitive manner.
Example 1:
var cityName = "new york";
var upperCaseCityName = cityName.toUpperCase();
Logger.log(upperCaseCityName); // Output: NEW YORK
In this example, we define a variable called cityName
and assign it the value "new york"
. We then call the toUpperCase() method on the cityName
variable and store the result in a new variable called upperCaseCityName
. Finally, we log the value of the upperCaseCityName
variable, which should output "NEW YORK"
.
Example 2:
function compareStrings(string1, string2) {
if (string1.toUpperCase() === string2.toUpperCase()) {
return true;
} else {
return false;
}
}
var result = compareStrings("apple", "APple");
Logger.log(result); // Output: true
In this example, we define a function called compareStrings
that takes two string parameters. We then call the toUpperCase() method on both strings when comparing them. This ensures that the comparison is done in a case-insensitive manner. Finally, we call the compareStrings
function with the strings "apple"
and "APple"
, which should return true
.