The String object's slice() method
The String object's slice()
method extracts a portion of a string and returns it as a new string, without modifying the original string.
Prerequisites
This tutorial assumes that you're familiar with:
Syntaxstring.slice(startIndex[, endIndex])
ParametersstartIndex:
The index at which to begin extraction. The first character is at index 0.
If negative, it is treated as str.length + startIndex
. For example, if startIndex
is -3, it is treated as str.length - 3
.
endIndex (optional):
string.slice(startIndex[, endIndex])
startIndex:
The index at which to begin extraction. The first character is at index 0.
If negative, it is treated as str.length + startIndex
. For example, if startIndex
is -3, it is treated as str.length - 3
.
The index at which to end extraction. The character at this index will not be included.
If omitted, slice() extracts through the end of the string.
If negative, it is treated as
str.length + endIndex
.
Return value
The slice()
method returns a new string containing the extracted section of the string.
ExamplesExample 1: Basic string slicing with positive indices
In the code below, the function sliceEx1()
demonstrates basic string slicing operations.
function sliceEx1() {
let str = "Hello World!";
// Extract "Hello" (characters from index 0 to 4)
Logger.log(str.slice(0, 5));
// Extract "World" (characters from index 6 to 10)
Logger.log(str.slice(6, 11));
// Extract from index 6 to the end
Logger.log(str.slice(6));
}
Output
Hello
World
World!
Example 2: String slicing with negative indices
In the code below, the function sliceEx2()
shows how negative indices work with slice()
.
function sliceEx2() {
let str = "Hello World!";
// Extract last 6 characters
Logger.log(str.slice(-6));
// Extract "World" using negative indices
Logger.log(str.slice(-6, -1));
// Extract everything except last character
Logger.log(str.slice(0, -1));
}
Output
World!
World
Hello World
Example 3: Common use cases for slice()
In the code below, the function sliceEx3()
demonstrates practical applications of the slice()
method.
function sliceEx3() {
// Truncate long text
let longText = "This is a very long string that needs to be truncated";
let truncated = longText.slice(0, 20) + "...";
Logger.log(truncated);
// Extract domain from email
let email = "user@example.com";
let domain = email.slice(email.indexOf("@") + 1);
Logger.log(domain);
// Extract file extension
let filename = "document.pdf";
let extension = filename.slice(filename.lastIndexOf("."));
Logger.log(extension);
}
Output
This is a very long…
example.com
Example 4: Demonstrating that slice() returns a new string
In the code below, the function sliceEx4()
shows how slice()
creates a new string without modifying the original.
function sliceEx4() {
let originalStr = "Hello World!";
// Create a new string using slice()
let slicedStr = originalStr.slice(0, 5);
// Log both strings to show original is unchanged
Logger.log("Original string: " + originalStr);
Logger.log("Sliced string: " + slicedStr);
// Prove they are separate strings
let slicedStr2 = slicedStr.toUpperCase();
Logger.log("Modified sliced string: " + slicedStr2);
Logger.log("Original string still unchanged: " + originalStr);
}
Output
Original string: Hello World!
Sliced string: Hello
Modified sliced string: HELLO
Original string still unchanged: Hello World!
Note
If startIndex
is greater than endIndex
, slice()
returns an empty string:
function sliceNote() {
let str = "Hello World!";
Logger.log(str.slice(5, 1)); // Returns empty string
}
Conclusion
In this tutorial, you learned how to use the slice()
method to extract portions of strings in Apps Script. The slice()
method is particularly useful for text processing tasks like truncation, extraction of substrings, and working with file names or email addresses.
DISCLAIMER: This content is provided for educational purposes only. All code, templates, and information should be thoroughly reviewed and tested before use. Use at your own risk. Full Terms of Service apply.
Small Scripts, Big Impact
Join 1,500+ professionals who are supercharging their productivity with Google Sheets automation
By subscribing, you agree to our Privacy Policy and Terms of Service