@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
str
: The string to trim.
Return value
- A new string with whitespace removed from the end.
Examples
Basic usage
// Remove whitespace from the endconst trimmed = String.trimEnd(" Hello world ");print(trimmed); // Outputs: " Hello world"
// Trim tabs and newlinesconst 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 uppercaseconst text = " hello world ";const processedText = String.toUpperCase(String.trimEnd(text));print(processedText); // Outputs: " HELLO WORLD"
Handling empty strings
// Trimming an empty stringconst empty = String.trimEnd("");print(empty); // Outputs: "" (empty string)
// Trimming a string with only whitespaceconst onlyWhitespace = String.trimEnd(" \t\n ");print(onlyWhitespace); // Outputs: "" (empty string)
Practical applications
// Clean up multi-line textfunction 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 trimEndconst endOnly = String.trimEnd(text);print(endOnly); // Outputs: " Hello world"
// Using trimconst bothEnds = String.trim(text);print(bothEnds); // Outputs: "Hello world"
// Using trimStartconst startOnly = String.trimStart(text);print(startOnly); // Outputs: "Hello world "