substr() method

The substr() method is used to extract a portion of a string based on a specified starting index and length. The method takes two arguments: the starting index and the length of the substring to be extracted.

For example, string.substr(2, 4) would extract a substring of four characters starting from the third character of the string.

Here are two examples of how to use the substr() method in Apps Script:

  1. Extracting the first name from a full name string:
var fullName = "John Doe";
var firstName = fullName.substr(0, fullName.indexOf(" "));

In this example, the substr() method is used to extract the portion of the fullName string before the first space character, which represents the first name.

  1. Extracting a portion of a string based on a dynamic length:
var str = "This is a test string";
var startIndex = 5;
var endIndex = 13;
var substr = str.substr(startIndex, endIndex - startIndex);

In this example, the substr() method is used to extract a portion of the str string based on a dynamic starting and ending index, resulting in a substring of a specific length.