@rbxts/jsnatives
trimStart
Removes whitespace from the beginning of a string.
Signature
function trimStart(str: string): string
Description
The trimStart
function removes whitespace characters from the beginning 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 beginning.
Examples
Basic usage
// Remove whitespace from the beginningconst trimmed = String.trimStart(" Hello world ");print(trimmed); // Outputs: "Hello world "
// Trim tabs and newlinesconst multiline = String.trimStart("\t\n Hello \n\t");print(multiline); // Outputs: "Hello \n\t"
Chaining with other string operations
// Trim start and then convert to uppercaseconst text = " hello world ";const processedText = String.toUpperCase(String.trimStart(text));print(processedText); // Outputs: "HELLO WORLD "
Handling empty strings
// Trimming an empty stringconst empty = String.trimStart("");print(empty); // Outputs: "" (empty string)
// Trimming a string with only whitespaceconst onlyWhitespace = String.trimStart(" \t\n ");print(onlyWhitespace); // Outputs: "" (empty string)
Practical applications
// Format code indentationfunction removeLeadingIndentation(code: string): string { const lines = String.split(code, "\n"); const trimmedLines = [];
for (const line of lines) { trimmedLines.push(String.trimStart(line)); }
return trimmedLines.join("\n");}
const code = " function hello() {\n print('Hello');\n }";const formatted = removeLeadingIndentation(code);print(formatted);// Outputs:// function hello() {// print('Hello');// }
Comparison with trim and trimEnd
const text = " Hello world ";
// Using trimStartconst startOnly = String.trimStart(text);print(startOnly); // Outputs: "Hello world "
// Using trimconst bothEnds = String.trim(text);print(bothEnds); // Outputs: "Hello world"
// Using trimEndconst endOnly = String.trimEnd(text);print(endOnly); // Outputs: " Hello world"