lcfirst
lcfirst(string $string): string
Makes a string's first character lowercase.
C C23
#include <ctype.h>
#include <stdio.h>
#include <string.h>
static void lcfirst(const char *s, char *out) {
strcpy(out, s);
if (out[0]) {
out[0] = (char)tolower((unsigned char)out[0]);
}
}
int main(void) {
char buf[64];
lcfirst("Hello", buf);
printf("%s\n", buf);
lcfirst("PHP", buf);
printf("%s\n", buf);
lcfirst("hello world", buf);
printf("%s\n", buf);
return 0;
}
hello pHP hello world
C++ C++26
#include <cctype>
#include <iostream>
#include <string>
static std::string lcfirst(std::string s) {
if (!s.empty()) {
s[0] = (char)std::tolower((unsigned char)s[0]);
}
return s;
}
int main() {
std::cout << lcfirst("Hello") << "\n" << lcfirst("PHP") << "\n" << lcfirst("hello world") << "\n";
return 0;
}
hello pHP hello world
C# v11.0.100
static string Lcfirst(string s) =>
s.Length == 0 ? s : char.ToLowerInvariant(s[0]) + s[1..];
Console.WriteLine(Lcfirst("Hello"));
Console.WriteLine(Lcfirst("PHP"));
Console.WriteLine(Lcfirst("hello world"));
hello pHP hello world
Go v1.27.4
package main
import (
"fmt"
"unicode"
)
func lcFirst(s string) string {
if s == "" {
return s
}
r := []rune(s)
r[0] = unicode.ToLower(r[0])
return string(r)
}
func main() {
fmt.Println(lcFirst("Hello"))
fmt.Println(lcFirst("PHP"))
fmt.Println(lcFirst("hello world"))
}
hello pHP hello world
Java v27.0.1
public class Main {
static String lcfirst(String s) {
if (s.isEmpty()) {
return s;
}
return Character.toLowerCase(s.charAt(0)) + s.substring(1);
}
public static void main(String[] args) {
System.out.println(lcfirst("Hello"));
System.out.println(lcfirst("PHP"));
System.out.println(lcfirst("hello world"));
}
}
hello pHP hello world
JavaScript v26.13.1
const lcfirst = (s) => s.charAt(0).toLowerCase() + s.slice(1); // no built-in lcfirst; hand-written idiom
console.log(lcfirst("Hello"));
console.log(lcfirst("PHP"));
console.log(lcfirst("hello world"));
hello pHP hello world
PHP v8.6.0
echo lcfirst("Hello"), "\n", lcfirst("PHP"), "\n", lcfirst("hello world");
hello pHP hello world
Python v3.15.2
def lcfirst(s):
return s[:1].lower() + s[1:] # Python has no built-in lcfirst; slicing idiom (safe on empty strings)
print(lcfirst("Hello"))
print(lcfirst("PHP"))
print(lcfirst("hello world"))
hello pHP hello world
Rust v1.99.1
fn lcfirst(s: &str) -> String {
let mut chars = s.chars();
match chars.next() {
Some(first) => first.to_lowercase().collect::<String>() + chars.as_str(),
None => String::new(),
}
}
fn main() {
println!("{}", lcfirst("Hello"));
println!("{}", lcfirst("PHP"));
println!("{}", lcfirst("hello world"));
}
hello pHP hello world
TypeScript v26.13.1
const lcfirst = (s: string): string => s.charAt(0).toLowerCase() + s.slice(1);
console.log(lcfirst("Hello"));
console.log(lcfirst("PHP"));
console.log(lcfirst("hello world"));
hello pHP hello world
Language differences
C: tolower((unsigned char)s[0]); C-family tolower is locale-sensitive (cast to unsigned char first).
C++: std::tolower; C-family tolower is locale-sensitive.
C#: char.ToLowerInvariant(s[0]) + s[1..]; use ToLowerInvariant for determinism.
Go: unicode.ToLower(r[0]) (rune-based, multibyte safe); decides by Unicode character properties, independent of locale.
Java: Character.toLowerCase(s.charAt(0)); decides by character properties, independent of locale.
JavaScript: charAt(0).toLowerCase() + slice(1).
PHP: lcfirst lowercases only the first character and leaves the rest alone (the partner of ucfirst); lowercasing a whole string belongs to the other API family (strtolower/toLowerCase/to_lower) — don't mix it with the first-character semantics.
Python: no same-named builtin — the idiom "lowercase the first character + concat the rest": s[:1].lower() + s[1:] (slicing is empty-string safe).
Rust: first.to_lowercase() + chars.as_str(); decides by character properties, independent of locale.
TypeScript: same as JS — charAt(0).toLowerCase() + slice(1).