I want to to put the first letter of a string into lowercase in D.
As a string is imutable in D, there doesn't seem to be a simple way.
I came up with this:
string mystr = "BookRef";
string outval = toLower( mystr[0..1] ) ~ mystr[1..$];
writeln( "my outval: ", outval );
Run Code Online (Sandbox Code Playgroud)
Is there an easier way ?
为了参考和完整性,您可以通过链接范围来构建它而无需任何分配。使用空字符串还具有其他优点:
auto downcase(string w)
{
import std.range, std.uni;
return w.take(1).asLowerCase.chain(w.drop(1));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
59 次 |
| 最近记录: |