方法1:

使用字符串切片和lower()方法

string s = "HelloWorld";
s = s[0].toLower() ~ s[1..$]; // $表示字符串s的长度
writeln(s); // 输出helloWorld

方法2:

使用std.algorithm.iteration.map和std.ascii.toLower

import std.algorithm.iteration;
import std.ascii;

string s = "HelloWorld";
s = s.map!(a => toLower(a)).array.joiner; // array.joiner把char[]转换为string
writeln(s); // 输出helloWorld

方法3:

使用std.string.chop和std.string.chopFront

import std.string;

string s = "HelloWorld";
s = s.chopFront.toLower() ~ s.chop;
writeln(s); // 输出helloWorld

方法4:

使用std.string.replace

import std.string;

string s = "HelloWorld";
s = replace(s, s[0..1], s[0..1].toLower());
writeln(s); // 输出helloWorld
``
dlang怎么让一个字符串首字母小写其他字母不变有几种实现方式给出每种方式代码

原文地址: http://www.cveoy.top/t/topic/flZG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录