@rbxts/jsnatives
padEnd
Pads the end of a string with a specified character until it reaches the desired length.
Signature
function padEnd(str: string, length: NumericAdmissible, fillString?: string): string
Description
The padEnd
function pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied to the end of the current string.
Parameters
str
: The string to pad.length
: The target length of the output string. If a string is provided, it will be converted to a number.fillString
(optional): The string to pad with. Default is a space (" ").
Return value
- A new string of the specified length with the fill string applied to the end.
- If the input string is already longer than the specified length, it is returned unchanged.
Examples
Basic usage
// Pad with spacesconst padded = String.padEnd("Hello", 10);print(padded); // Outputs: "Hello " (5 spaces added)
// Pad with a custom characterconst paddedStars = String.padEnd("Hello", 10, "*");print(paddedStars); // Outputs: "Hello*****"
Using multiple characters for padding
// Fill string can be more than one characterconst paddedPattern = String.padEnd("Hi", 10, "-.");print(paddedPattern); // Outputs: "Hi-.-.-.-."
Handling edge cases
// String already longer than target lengthconst noChange = String.padEnd("Hello world", 5);print(noChange); // Outputs: "Hello world" (no padding added)
// Empty fill string defaults to spaceconst defaultFill = String.padEnd("Hello", 10, "");print(defaultFill); // Outputs: "Hello " (uses space as default)
// Target length less than 1const invalidLength = String.padEnd("Hello", -5);print(invalidLength); // Outputs: "Hello" (no padding)
Using string length parameter
// String length will be converted to numberconst paddedNumeric = String.padEnd("Hello", "8");print(paddedNumeric); // Outputs: "Hello "
Practical applications
// Create a fixed-width column for text alignmentfunction createColumn(text: string, width: number): string { return String.padEnd(text, width);}
const col1 = createColumn("Name", 15);const col2 = createColumn("Age", 5);const col3 = createColumn("City", 15);
print(`${col1}${col2}${col3}`);// Outputs: "Name Age City "
// Building a simple progress barfunction progressBar(percent: number, length: number = 20): string { const filled = Math.floor(percent / 100 * length); const empty = length - filled;
return String.padEnd(String.padEnd("", filled, "█"), length, "░");}
print(progressBar(0)); // Outputs: "░░░░░░░░░░░░░░░░░░░░"print(progressBar(50)); // Outputs: "██████████░░░░░░░░░░"print(progressBar(100)); // Outputs: "████████████████████"
Comparison with padStart and padBoth
const text = "Hello";
// Using padEndconst endPadded = String.padEnd(text, 10, "-");print(endPadded); // Outputs: "Hello-----"
// Using padStartconst startPadded = String.padStart(text, 10, "-");print(startPadded); // Outputs: "-----Hello"
// Using padBothconst bothPadded = String.padBoth(text, 10, "-");print(bothPadded); // Outputs: "--Hello--" or similar (balanced)