str_pad
str_pad(string $string, int $length, string $pad_string = " ", int $pad_type = STR_PAD_RIGHT): string
Pads a string to a given length with another string.
C C23
#include <stdio.h>
#include <string.h>
static void pad(char *dst, const char *s, char fill, int width, int left) {
int n = width - (int)strlen(s);
int i = 0;
if (!left) {
memcpy(dst, s, strlen(s));
i = (int)strlen(s);
}
memset(dst + i, fill, n);
i += n;
if (left) {
memcpy(dst + i, s, strlen(s));
i += (int)strlen(s);
}
dst[i] = '\0';
}
int main(void) {
char buf[16];
pad(buf, "A", '.', 5, 0);
printf("%s\n", buf);
pad(buf, "A", '.', 5, 1);
printf("%s\n", buf);
return 0;
}
A.... ....A
C++ C++26
#include <iostream>
#include <string>
int main() {
std::string s = "A";
std::cout << s + std::string(5 - s.size(), '.') << "\n";
std::cout << std::string(5 - s.size(), '.') + s << "\n";
return 0;
}
A.... ....A
C# v11.0.100
Console.WriteLine("A".PadRight(5, '.'));
Console.WriteLine("A".PadLeft(5, '.'));
A.... ....A
Go v1.27.4
package main
import (
"fmt"
"strings"
)
func padLeft(s, pad string, width int) string {
return strings.Repeat(pad, width-len(s)) + s
}
func padRight(s, pad string, width int) string {
return s + strings.Repeat(pad, width-len(s))
}
func main() {
fmt.Println(padRight("A", ".", 5))
fmt.Println(padLeft("A", ".", 5))
}
A.... ....A
Java v27.0.1
public class Main {
private static String pad(String s, String pad, int width, boolean left) {
String fill = pad.repeat(width - s.length());
return left ? fill + s : s + fill;
}
public static void main(String[] args) {
System.out.println(pad("A", ".", 5, false));
System.out.println(pad("A", ".", 5, true));
}
}
A.... ....A
JavaScript v26.13.1
console.log("A".padEnd(5, "."));
console.log("A".padStart(5, "."));
A.... ....A
PHP v8.6.0
echo str_pad("A", 5, "."), "\n";
echo str_pad("A", 5, ".", STR_PAD_LEFT), "\n";
echo str_pad("A", 6, "-", STR_PAD_BOTH);
A.... ....A --A---
Python v3.15.2
print("A".ljust(5, "."))
print("A".rjust(5, "."))
print("A".center(6, "-"))
A.... ....A --A---
Rust v1.99.1
fn main() {
let s = "A";
let fill = ".".repeat(5 - s.len());
println!("{}{}", s, fill);
println!("{}{}", fill, s);
}
A.... ....A
TypeScript v26.13.1
console.log("A".padEnd(5, "."));
console.log("A".padStart(5, "."));
A.... ....A
Language differences
C: assembles with memcpy+memset.
C++: constructs the fill with std::string(count, ch).
C#: PadRight/PadLeft are single-sided only, with no centering.
Go: no built-in custom-character padding (fmt's %*s pads spaces only) — the example hand-rolls with strings.Repeat.
Java: no built-in custom-character padding (String.repeat can compose it) — the example hand-rolls.
JavaScript: padEnd/padStart are single-sided only, with no centering.
PHP: str_pad covers right/left/both padding in one function (STR_PAD_BOTH floors the left half, so padding A to width 6 with - yields --A---).
Python: ljust/rjust/center map one to one (center floors the left half too).
Rust: no built-in custom-character padding — the example hand-rolls with .repeat.
TypeScript: same as JS — padEnd/padStart.