lcfirst

lcfirst(string $string): string

将字符串的首字符转换为小写。

C C23

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

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

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

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

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

v26.13.1
const lcfirst = (s) => s.charAt(0).toLowerCase() + s.slice(1); // 无内置 lcfirst,手写惯用法
console.log(lcfirst("Hello"));
console.log(lcfirst("PHP"));
console.log(lcfirst("hello world"));
输出
hello
pHP
hello world

PHP v8.6.0

v8.6.0
echo lcfirst("Hello"), "\n", lcfirst("PHP"), "\n", lcfirst("hello world");
输出
hello
pHP
hello world

Python v3.15.2

v3.15.2
def lcfirst(s):
    return s[:1].lower() + s[1:]  # Python 无内置 lcfirst,切片惯用法(对空串安全)


print(lcfirst("Hello"))
print(lcfirst("PHP"))
print(lcfirst("hello world"))
输出
hello
pHP
hello world

Rust v1.99.1

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

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

语言差异说明

C:tolower((unsigned char)s[0]);C 系 tolower 受 locale 影响(传 unsigned char 转换后再用)。

C++:std::tolower;C 系 tolower 受 locale 影响。

C#:char.ToLowerInvariant(s[0]) + s[1..];要确定性用 ToLowerInvariant。

Go:unicode.ToLower(r[0])(按 rune,多字节安全);按字符属性判定、独立于 locale。

Java:Character.toLowerCase(s.charAt(0));按字符属性判定、独立于 locale。

JavaScript:charAt(0).toLowerCase() + slice(1)。

PHP:lcfirst 只把首个字符转小写、其余原样(与 ucfirst 成对);整串转小写是另一族 API(strtolower/toLowerCase/to_lower),别与首字符语义混用。

Python:无同名内置,惯用「首字符小写 + 剩余拼接」手写:s[:1].lower() + s[1:](切片对空串安全)。

Rust:first.to_lowercase() + chars.as_str();按字符属性判定、独立于 locale。

TypeScript:同 JS,charAt(0).toLowerCase() + slice(1)。