@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

Return value

Examples

Basic usage

// Pad with spaces
const padded = String.padEnd("Hello", 10);
print(padded); // Outputs: "Hello " (5 spaces added)
// Pad with a custom character
const paddedStars = String.padEnd("Hello", 10, "*");
print(paddedStars); // Outputs: "Hello*****"

Using multiple characters for padding

// Fill string can be more than one character
const paddedPattern = String.padEnd("Hi", 10, "-.");
print(paddedPattern); // Outputs: "Hi-.-.-.-."

Handling edge cases

// String already longer than target length
const noChange = String.padEnd("Hello world", 5);
print(noChange); // Outputs: "Hello world" (no padding added)
// Empty fill string defaults to space
const defaultFill = String.padEnd("Hello", 10, "");
print(defaultFill); // Outputs: "Hello " (uses space as default)
// Target length less than 1
const invalidLength = String.padEnd("Hello", -5);
print(invalidLength); // Outputs: "Hello" (no padding)

Using string length parameter

// String length will be converted to number
const paddedNumeric = String.padEnd("Hello", "8");
print(paddedNumeric); // Outputs: "Hello "

Practical applications

// Create a fixed-width column for text alignment
function 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 bar
function 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 padEnd
const endPadded = String.padEnd(text, 10, "-");
print(endPadded); // Outputs: "Hello-----"
// Using padStart
const startPadded = String.padStart(text, 10, "-");
print(startPadded); // Outputs: "-----Hello"
// Using padBoth
const bothPadded = String.padBoth(text, 10, "-");
print(bothPadded); // Outputs: "--Hello--" or similar (balanced)