mb_ucfirst / mb_lcfirst
mb_ucfirst(string $string, ?string $encoding = null): string — mb_lcfirst(string $string, ?string $encoding = null): string
Multibyte-safe first-letter case conversion (PHP 8.4+) - mbstring's answer to ucfirst/lcfirst being ASCII-only: multibyte letters like z with caron finally convert.
C C23
#include <ctype.h>
#include <stdio.h>
int main(void) {
/* toupper is byte-oriented: ASCII works, multibyte letters are out
of scope - C has no standard case mapping beyond the C locale */
char s[] = "hello";
s[0] = (char) toupper((unsigned char) s[0]);
printf("%s\n", s); /* Hello */
/* turning z-with-caron into Z-with-caron needs ICU or gettext:
the standard library stops at single bytes */
return 0;
}
Hello
C++ C++26
#include <cctype>
#include <iostream>
#include <string>
int main() {
std::string s = "hello";
s[0] = static_cast<char>(std::toupper(static_cast<unsigned char>(s[0]))); // byte-oriented, ASCII only
std::cout << s << "\n"; // Hello
// multibyte case mapping (z-with-caron -> Z-with-caron) needs ICU:
// the standard library stops at bytes, exactly like C
return 0;
}
Hello
C# v11.0.100
Console.WriteLine(UpperFirst("hello"));
Console.WriteLine(UpperFirst("\u017Eyrafa")); // z-with-caron: char is Unicode-aware
Console.WriteLine(UpperFirst("\u4F60\u597D")); // caseless script passes through
static string UpperFirst(string s) =>
s.Length == 0 ? s : char.ToUpperInvariant(s[0]) + s.Substring(1);
Hello Žyrafa 你好
Go v1.27.4
package main
import (
"fmt"
"unicode"
"unicode/utf8"
)
func upperFirst(s string) string {
r, size := utf8.DecodeRuneInString(s)
if r == utf8.RuneError && size <= 1 {
return s // invalid UTF-8: pass through unchanged
}
return string(unicode.ToUpper(r)) + s[size:]
}
func main() {
fmt.Println(upperFirst("hello")) // Hello
fmt.Println(upperFirst("žyrafa")) // Žyrafa: runes are Unicode-aware
fmt.Println(upperFirst("你好")) // caseless scripts unchanged
}
Hello Žyrafa 你好
Java v27.0.1
public class Main {
static String upperFirst(String s) {
if (s.isEmpty()) return s;
int first = s.codePointAt(0);
return new String(Character.toChars(Character.toUpperCase(first)))
+ s.substring(Character.charCount(first));
}
public static void main(String[] args) {
System.out.println(upperFirst("hello")); // Hello
System.out.println(upperFirst("žyrafa")); // Žyrafa: code-point aware
System.out.println(upperFirst("你好")); // caseless unchanged
}
}
Hello Žyrafa 你好
JavaScript v26.13.1
// no builtin first-letter case helpers - the slicing idiom is standard
const upperFirst = (s) => s.charAt(0).toUpperCase() + s.slice(1);
const lowerFirst = (s) => s.charAt(0).toLowerCase() + s.slice(1);
console.log(upperFirst("hello")); // Hello
console.log(upperFirst("žyrafa")); // Žyrafa: toUpperCase is Unicode-aware
console.log(lowerFirst("ŻYRAFA")); // żYRAFA
// caveat: surrogate-pair first characters need codePointAt, not charAt
Hello Žyrafa żYRAFA
PHP v8.6.0
// PHP 8.4: the mbstring ticket that ucfirst/lcfirst owed for 25 years
var_dump(mb_ucfirst('hello')); // Hello
var_dump(mb_lcfirst('HELLO')); // hELLO
var_dump(mb_ucfirst('žyrafa')); // Žyrafa: multibyte letter converted
var_dump(ucfirst('žyrafa')); // ucfirst cannot touch non-ASCII: unchanged
var_dump(mb_lcfirst('ŻYRAFA')); // żYRAFA
var_dump(lcfirst('ŻYRAFA')); // Ż is not ASCII: unchanged too
string(5) "Hello" string(5) "hELLO" string(7) "Žyrafa" string(7) "žyrafa" string(7) "żYRAFA" string(7) "ŻYRAFA"
Python v3.15.2
# no builtin ucfirst either - the slicing idiom is standard
print("hello"[:1].upper() + "hello"[1:]) # Hello
print("žyrafa"[:1].upper() + "žyrafa"[1:]) # Žyrafa: str.upper is Unicode-aware
print("ŻYRAFA"[:1].lower() + "ŻYRAFA"[1:]) # żYRAFA
# str.capitalize() uppercases the first char AND lowercases the rest -
# a different contract from ucfirst
print("hELLO".capitalize()) # Hello
Hello Žyrafa żYRAFA Hello
Rust v1.99.1
fn upper_first(s: &str) -> String {
let mut chars = s.chars();
match chars.next() {
Some(c) => c.to_uppercase().collect::<String>() + chars.as_str(),
None => String::new(),
}
}
fn main() {
println!("{}", upper_first("hello")); // Hello
println!("{}", upper_first("žyrafa")); // Žyrafa: char is Unicode-aware
println!("{}", upper_first("你好")); // caseless unchanged
// no stdlib lowercase-first either; the symmetric idiom uses to_lowercase
}
Hello Žyrafa 你好
TypeScript v26.13.1
// no builtin first-letter case helpers - the slicing idiom is standard
const upperFirst = (s: string): string => s.charAt(0).toUpperCase() + s.slice(1);
const lowerFirst = (s: string): string => s.charAt(0).toLowerCase() + s.slice(1);
console.log(upperFirst("hello")); // Hello
console.log(upperFirst("žyrafa")); // Žyrafa
console.log(lowerFirst("ŻYRAFA")); // żYRAFA
Hello Žyrafa żYRAFA
Language differences
C: toupper/tolower are byte-oriented and locale-bound - in the C locale only A-Z move; multibyte letters (z-with-caron) have no standard mapping, and ICU/gettext is required; the example can only demo ASCII.
C++: same as C - std::toupper is byte-oriented with the ASCII limitation; no standard multibyte case mapping, and ICU is the real-world choice.
C#: the char.ToUpperInvariant(s[0]) + Substring(1) idiom (example); char is a UTF-16 code unit - BMP Latin extensions (z-with-caron) convert fine, while surrogate-pair first characters need a char.IsHighSurrogate branch; the culture-sensitive ToUpper has the Turkish-i problem, so Invariant is the idiom.
Go: no builtin - utf8.DecodeRuneInString for the first rune plus unicode.ToUpper and concatenation (example); runes are natively Unicode-aware, so z-with-caron -> Z-with-caron just works; invalid UTF-8 needs an explicit short-circuit (the RuneError branch in the example).
Java: no builtin - the codePointAt(0) + Character.toUpperCase + charCount idiom (example); code-point APIs handle surrogate pairs correctly; caseless scripts pass through unchanged.
JavaScript: no builtin - the slicing idiom charAt(0).toUpperCase() + slice(1) (example); toUpperCase is natively Unicode-aware; a surrogate-pair first character (like an emoji) needs the codePointAt version, otherwise the character is split in half.
PHP: mb_ucfirst/mb_lcfirst are NEW in PHP 8.4 (verified with function_exists first) - ucfirst/lcfirst have been ASCII-only since PHP 3 (the example returns z-with-caron unchanged), and this 25-year ticket finally cashed in; semantics: the first character gets a Unicode case mapping under the encoding, the rest untouched; caseless scripts (Chinese) return unchanged - first-letter casing is NOT capitalize (it never lowercases the rest).
Python: no builtin ucfirst - the slicing idiom (example); str.upper/lower are natively Unicode-aware; note that str.capitalize() has different semantics (it also lowercases the rest).
Rust: no builtin - the chars().next() + to_uppercase idiom (example); to_uppercase returns an iterator (handling multi-character mappings like sharp-s to SS) which is collected and concatenated; caseless scripts unchanged.
TypeScript: same as JavaScript.