@rbxts/jsnatives



trimEnd

Removes whitespace from the end of a string.

Signature

function trimEnd(str: string): string

Description

The trimEnd function removes whitespace characters from the end of a string, and returns a new string. Whitespace includes spaces, tabs, newlines, and other whitespace characters.

Parameters

Return value

Examples

Basic usage

// Remove whitespace from the end
const trimmed = String.trimEnd(" Hello world ");
print(trimmed); // Outputs: " Hello world"
// Trim tabs and newlines
const multiline = String.trimEnd("\t\n Hello \n\t");
print(multiline); // Outputs: "\t\n Hello"

Chaining with other string operations

// Trim end and then convert to uppercase
const text = " hello world ";
const processedText = String.toUpperCase(String.trimEnd(text));
print(processedText); // Outputs: " HELLO WORLD"

Handling empty strings

// Trimming an empty string
const empty = String.trimEnd("");
print(empty); // Outputs: "" (empty string)
// Trimming a string with only whitespace
const onlyWhitespace = String.trimEnd(" \t\n ");
print(onlyWhitespace); // Outputs: "" (empty string)

Practical applications

// Clean up multi-line text
function removeTrailingNewlines(text: string): string {
return String.trimEnd(text);
}
const multilineText = "Hello\nWorld\n\n\n";
const cleaned = removeTrailingNewlines(multilineText);
print(cleaned); // Outputs: "Hello\nWorld"

Comparison with trim and trimStart

const text = " Hello world ";
// Using trimEnd
const endOnly = String.trimEnd(text);
print(endOnly); // Outputs: " Hello world"
// Using trim
const bothEnds = String.trim(text);
print(bothEnds); // Outputs: "Hello world"
// Using trimStart
const startOnly = String.trimStart(text);
print(startOnly); // Outputs: "Hello world "