资讯动态

Humanizer.CasingExtensions 详解:用 ApplyCase 轻松切换字符串的四种大小写风格

发布时间:2026/9/29 3:25:25 来源:尧图企业网站定制
开发工具【免费下载链接】HumanizerHumanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities项目地址https://gitcode.com/gh_mirrors/hu/Humanizer点击查看免费下载导读CasingExtensions是 Humanizer 库中负责字符串大小写变换的静态扩展类核心只有一个方法ApplyCase(this string, LetterCasing)。它能一句话把任意字符串切换为标题式Title、全小写LowerCase、全大写AllCaps或句首大写Sentence四种风格并自动适配当前线程的区域文化Culture。读完本文你将掌握ApplyCase的完整签名、四种LetterCasing枚举值的语义差异、其底层 Transformer 实现原理以及它在Humanize、Titleize、枚举人性化等 Humanizer 常用 API 中的真实调用场景。类定位与文档骨架本文档对应的 API 参考页面为 Humanizer.CasingExtensions.md对应的源码实现在 src/Humanizer/CasingExtensions.cs。类声明如下public static class CasingExtensions它是System.Object的直接派生静态类通过扩展方法机制挂接到string类型上因此无需任何额外配置即可在项目引用 Humanizer 后直接使用some string.ApplyCase(LetterCasing.Title)。ApplyCase 方法签名与参数完整签名public static string ApplyCase(this string input, Humanizer.LetterCasing casing);参数说明参数类型说明inputSystem.String待变换的字符串必须非 null否则调用方会触发空引用异常casingHumanizer.LetterCasing期望应用的目标大小写风格枚举返回值System.String—— 应用指定大小写风格后的新字符串。原字符串不会被修改字符串不可变性四个枚举值对应的输出语义如下LetterCasing.Title每个单词首字母大写如some string→Some StringLetterCasing.LowerCase全部字母小写如SOME STRING→some stringLetterCasing.AllCaps全部字母大写如some string→SOME STRINGLetterCasing.Sentence仅首字符大写、其余保持不变如some string→Some string。异常当传入的LetterCasing值不在枚举定义范围内时例如强转(LetterCasing)99方法抛出System.ArgumentOutOfRangeException。LetterCasing 枚举四种风格的精确含义枚举定义位于 src/Humanizer/LetterCasing.cs其 XML 注释给出直观的输入输出映射枚举成员语义示例源注释Title每个单词首字母大写SomeString→Some StringAllCaps全部字母大写SomeString→SOME STRINGLowerCase全部字母小写SomeString→some stringSentence仅首字母大写、其余保持原样SomeString→Some string注意Sentence与常见句首大写、其余一律小写的朴素实现不同Humanizer 的ToSentenceCase在首字符已是大写时直接返回原字符串见下文原理这也是测试用例Sentence casing → Sentence casing、honors UPPER case → Honors UPPER case所验证的行为——中间的UPPER不会被强行改小写。官方示例与测试验证文档给出的四个标准示例见 Humanizer.CasingExtensions.mdsome string.ApplyCase(LetterCasing.Title) Some String SOME STRING.ApplyCase(LetterCasing.LowerCase) some string some string.ApplyCase(LetterCasing.AllCaps) SOME STRING some string.ApplyCase(LetterCasing.Sentence) Some string这些行为在单元测试 tests/Humanizer.Tests/CasingTests.cs 中均有[Theory]数据驱动用例覆盖。除官方示例外测试还揭示了两个容易被忽视的细节标题式会保留全大写缩写honors UPPER case.ApplyCase(LetterCasing.Title)→Honors UPPER Case即已经是全大写的单词不会被改写成小写标题式正确处理括号title case (with parenthesis)→Title Case (With Parenthesis)括号内的单词同样参与首字母大写。底层实现原理switch 分发到 Transformer 体系ApplyCase的实现非常精简本质上是一个switch表达式把四种枚举值分别映射到To门户类src/Humanizer/Transformer/To.cs暴露的四个ICulturedStringTransformer实例上再通过input.Transform(...)执行public static string ApplyCase(this string input, LetterCasing casing) casing switch { LetterCasing.Title input.Transform(To.TitleCase), LetterCasing.LowerCase input.Transform(To.LowerCase), LetterCasing.AllCaps input.Transform(To.UpperCase), LetterCasing.Sentence input.Transform(To.SentenceCase), _ throw new ArgumentOutOfRangeException(nameof(casing)) };从源码结构看这四条路径分别对应四个 Transformer 类它们全部实现了ICulturedStringTransformer支持CultureInfo重载因此大小写变换天然具备区域文化感知能力ToTitleCaseToTitleCase.cs最复杂的实现。对纯 ASCII 输入走手写快速路径TryTransformAscii避免正则开销对含非 ASCII 字符的输入回退到正则路径TransformWithRegex。两条路径都遵循两条规则全大写的单词如缩写原样保留除首个单词外冠词/连词/介词a, an, as, at, by, if, in, of, on, or, so, to, up, and, but, for, nor, off, the, via, yet保持小写。此外对土耳其语tr和阿塞拜疆语az文化ASCII 大小写转换会改用文化敏感路径正确处理i/İToSentenceCaseToSentenceCase.cs若首字符已是char.IsUpper则原样返回否则仅将首字符用culture.TextInfo.ToUpper大写剩余部分逐字节保持不变ToLowerCaseToLowerCase.cs直接调用culture.TextInfo.ToLower(input)ToUpperCaseToUpperCase.cs直接调用culture.TextInfo.ToUpper(input)。To.Transform本身src/Humanizer/Transformer/To.cs使用Aggregate按顺序叠加多个 transformer这为将来串联多个变换如先Humanize再TitleCase保留了扩展点。在 Humanizer 生态中的真实调用场景ApplyCase并非孤立存在它是 Humanizer 多个高层 API 的公共底座Humanize(this string input, LetterCasing casing)定义在 StringHumanizeExtensions.cs内部就是input.Humanize().ApplyCase(casing)的组合XML 注释明确说明这是Humanize(string)与ApplyCase的便捷封装。测试 StringHumanizeTests.cs 覆盖了土耳其语标题化istanbulInputString → İstanbul Input String、德语全小写STRAẞE → straße、法语全大写coûts_privés → COÛTS PRIVÉS等文化相关用例Titleize()定义在 InflectorExtensions.cs先Humanize()再把结果ApplyCase(LetterCasing.Title)如some-package_name.Titleize() Some Package Name枚举人性化EnumHumanizeExtensions在生成人性化文本后同样调用.ApplyCase(enumCasing)应用目标大小写见 EnumHumanizeExtensions.cs。快速上手示例在项目引用 Humanizer 包后默认启用隐式全局 using可直接编写using Humanizer; string raw hello WORLD; Console.WriteLine(raw.ApplyCase(LetterCasing.Title)); // Hello WORLD全大写缩写保留 Console.WriteLine(raw.ApplyCase(LetterCasing.LowerCase)); // hello world Console.WriteLine(raw.ApplyCase(LetterCasing.AllCaps)); // HELLO WORLD Console.WriteLine(raw.ApplyCase(LetterCasing.Sentence)); // Hello WORLD首字符大写其余不变 // 组合用法先人性化拆分再套用大小写 some_pascal_case.Humanize(LetterCasing.Sentence); // Some pascal case product_category_id.Humanize(LetterCasing.Title); // Product Category Id注意事项小结input为 null 时调用ApplyCase会抛出NullReferenceException文档与源码均注明必须非 null非法枚举值会抛出ArgumentOutOfRangeExceptionSentence模式只改首字符不会像Title那样重排其余单词大小写Title模式会跳过全大写单词与停用词冠词/连词/介词大小写转换遵循当前CultureInfo.CurrentCulture在土耳其语、阿塞拜疆语等特殊文化环境下行为会相应调整。赞分享开发工具【免费下载链接】HumanizerHumanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities项目地址https://gitcode.com/gh_mirrors/hu/Humanizer点击查看免费下载相关推荐Humanizer CasingExtensions 使用指南用 ApplyCase 轻松统一字符串大小写风格Humanizer CasingExtensions 使用指南用 ApplyCase 轻松统一字符串大小写风格 Humanizer 的 CasingExten开发工具Humanizer CasingExtensions 完全指南用 ApplyCase 统一控制字符串大小写风格Humanizer CasingExtensions 完全指南用 ApplyCase 统一控制字符串大小写风格 Humanizer 的 CasingExten开发工具Humanizer CasingExtensions.ApplyCase 指南用 LetterCasing 一键切换字符串大小写风格Humanizer CasingExtensions.ApplyCase 指南用 LetterCasing 一键切换字符串大小写风格 本篇技术指南围绕 Hum开发工具上一篇drawio-desktop 快速上手指南免费跨平台流程图工具3 步从画到导出下一篇d3dxSkinManage 新手教程三步让 3DMigoto 皮肤 MOD 井然有序创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑