1. 使用std.algorithm中的函数
import std.algorithm : map, filter, join, split;
import std.string : toUpper, toLower;

string toCamelCase(string input)
{
    auto parts = input.split(["_", "-", " "]).filter!(a => a.length > 0).map!(a => a.toLower());
    return parts.front() ~ parts[1..$].map!(a => a.front().toUpper() ~ a[1..$]).join("");
}
  1. 使用正则表达式
import std.regex;

string toCamelCase(string input)
{
    auto re = regex(`[_\- ]([a-z])`);
    return input.replace(re, (m) => m[1].toUpper);
}
  1. 使用循环遍历
string toCamelCase(string input)
{
    string result;
    bool upperNext = false;
    foreach (char c; input)
    {
        if (c == '_' || c == '-' || c == ' ')
        {
            upperNext = true;
        }
        else if (upperNext)
        {
            result ~= toUpper(c);
            upperNext = false;
        }
        else
        {
            result ~= c;
        }
    }
    return result;
}
``
dlang语言实现将给定字符串转为驼峰命名的形式有点类似php的laravel框架常用代码 aa_bb-cc dd eeoo ---AaBbCcDdEeoo 空格_-都将替换 access public param string $value return string public static function s

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

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