Dlang 字符串首字母小写:两种简单实现方式
Dlang 提供多种方式将字符串首字母转换为小写。以下是两种常见的方法:
- 使用
std.string.toLower函数将字符串转换为小写,再使用std.string.capitalize函数将首字母转换为大写:
import std.string;
string str = 'Hello World';
str = str.toLower().capitalize();
writeln(str); // 输出:Hello world
- 直接对字符串的第一个字符进行转换:
string str = 'Hello World';
if (!str.empty) {
str[0] = toLower(str[0]);
}
writeln(str); // 输出:hello World
注意:第二种方式需要确保字符串非空,否则会出现运行时错误。
原文地址: https://www.cveoy.top/t/topic/octl 著作权归作者所有。请勿转载和采集!