ucfirst
ucfirst(string $string): string
Converts the first letter of a string to uppercase.
C C23
C23
#include <ctype.h>
#include <stdio.h>
int main(void) {
const char *s = "hello world";
printf("%c%s\n", toupper((unsigned char)s[0]), s + 1);
return 0;
}
Output
Hello world
C++ C++26
C++26
#include <cctype>
#include <iostream>
#include <string>
int main() {
std::string s = "hello world";
s[0] = (char)std::toupper((unsigned char)s[0]);
std::cout << s << "\n";
return 0;
}
Output
Hello world
C# v11.0.100
v11.0.100
var s = "hello world";
Console.WriteLine(char.ToUpperInvariant(s[0]) + s.Substring(1));
Output
Hello world
Go v1.27.4
v1.27.4
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.ToUpper("hello world"[:1]) + "hello world"[1:])
}
Output
Hello world
Java v27.0.1
v27.0.1
public class Main {
public static void main(String[] args) {
String s = "hello world";
System.out.println(s.substring(0, 1).toUpperCase() + s.substring(1));
}
}
Output
Hello world
JavaScript v26.13.1
v26.13.1
console.log("hello world"[0].toUpperCase() + "hello world".slice(1))
Output
Hello world
PHP v8.6.0
v8.6.0
echo ucfirst("hello world");
Output
Hello world
Python v3.15.2
v3.15.2
print("hello world".capitalize())
Output
Hello world
Rust v1.99.1
v1.99.1
fn main() {
let s = "hello world";
let mut chars = s.chars();
match chars.next() {
// uppercase first char + concat the rest; chars.next() is None for an empty string
Some(first) => println!("{}{}", first.to_uppercase(), chars.as_str()),
None => println!(""),
}
}
Output
Hello world
TypeScript v26.13.1
v26.13.1
const s: string = "hello world";
console.log(s[0].toUpperCase() + s.slice(1));
Output
Hello world
Language differences
C: uppercases the first character with toupper().
Go: no built-in — splice the first letter manually.
Java: no built-in — splice the first letter manually.
JavaScript: no built-in — splice the first letter manually.
PHP: ucfirst uppercases only the first letter and leaves the rest untouched.
Python: capitalize() also lowercases the remaining letters (HELLO WORLD -> Hello world).
Rust: no built-in ucfirst — uppercase the first char with to_uppercase() and splice the rest (the example handles the empty-string boundary).