尧图网络科技YAOTU DIGITAL 获取报价
获取报价
首页 / 资讯中心 / 文章详情

Humanizer 的 LetterCasing 枚举:四种字符串大小写转换的完整指南

发布时间:2026/9/27 9:03:20

资讯中心
01
ARTICLE

Humanizer 的 LetterCasing 枚举:四种字符串大小写转换的完整指南

Humanizer 的 LetterCasing 枚举:四种字符串大小写转换的完整指南
开发工具【免费下载链接】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点击查看免费下载导读LetterCasing是 Humanizer 库中定义输出字符串大小写风格的核心枚举它把标题式、全大写、全小写、句首大写这四种常见的文本转换需求统一抽象为类型安全的枚举值。在 Humanizer 中它被ApplyCase扩展方法、Humanize(LetterCasing)系列重载以及枚举人性化等场景广泛使用。读完本文你将掌握每个枚举值的精确行为、底层转换器实现原理、文化感知culture-aware细节以及如何在字符串与枚举人性化流水线中组合使用它们。枚举定义与四个取值LetterCasing定义在 src/Humanizer/LetterCasing.cs是Humanizer命名空间下的一个公共枚举官方文档对其的定位是用于指定输出字符串所需字母大小写风格的选项namespace Humanizer; /// summary /// Options for specifying the desired letter casing for the output string /// /summary public enum LetterCasing { /// summary /// SomeString - Some String /// /summary Title, /// summary /// SomeString - SOME STRING /// /summary AllCaps, /// summary /// SomeString - some string /// /summary LowerCase, /// summary /// SomeString - Some string /// /summary Sentence, }四个成员的底层数值分别为0、1、2、3其语义与示例对照如下以SomeString为输入枚举值数值转换效果说明Title0SomeString→Some String每个单词首字母大写标题式AllCaps1SomeString→SOME STRING全部转为大写LowerCase2SomeString→some string全部转为小写Sentence3SomeString→Some string仅句首字符大写其余保持不变从源码结构看该枚举没有显式指定数值因此按 C# 枚举默认规则成员从0开始依次递增与 website/versioned_docs/version-2.14.1/api/Humanizer.LetterCasing.md 文档中标注的字段值完全一致。核心入口CasingExtensions.ApplyCaseLetterCasing枚举本身只表达意图真正执行转换的是 src/Humanizer/CasingExtensions.cs 中的ApplyCase扩展方法。它是面向普通字符串的最直接入口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)) };该方法在源码注释中给出的行为契约如下some string.ApplyCase(LetterCasing.Title)→Some StringSOME STRING.ApplyCase(LetterCasing.LowerCase)→some stringsome string.ApplyCase(LetterCasing.AllCaps)→SOME STRINGsome string.ApplyCase(LetterCasing.Sentence)→Some string非法枚举值的处理注意ApplyCase的 switch 表达式带有_ 兜底分支会抛出ArgumentOutOfRangeException。这并非理论上的防御代码——tests/Humanizer.Tests/CoverageGapTests.cs 中就有专门测试验证Assert.ThrowsArgumentOutOfRangeException(() hello.ApplyCase((LetterCasing)42));即任何未定义的枚举数值例如强转的42都会触发异常确保不会静默产生未定义的大小写行为。底层实现四种 Transformer 的文化感知转换ApplyCase内部委托给 src/Humanizer/Transformer/To.cs 暴露的四个静态转换器属性To.TitleCase、To.LowerCase、To.UpperCase、To.SentenceCase。它们都实现了ICulturedStringTransformer接口因此全部支持传入CultureInfo进行文化感知转换——这是 Humanizer 大小写转换与 .NET 原生 API 相比的重要差异点。LowerCase / UpperCase基于 TextInfo 的全量转换src/Humanizer/Transformer/ToLowerCase.cs 与 src/Humanizer/Transformer/ToUpperCase.cs 的实现非常直白直接委托给CultureInfo.TextInfopublic string Transform(string input, CultureInfo culture) culture.TextInfo.ToLower(input); // ToLowerCase // culture.TextInfo.ToUpper(input); // ToUpperCaseTextInfo.ToLower/ToUpper会依据当前文化如en-US、tr-TR的规则进行大小写映射从而正确处理带口音字符等 Unicode 文本。Sentence仅处理首字符其余原样保留src/Humanizer/Transformer/ToSentenceCase.cs 的行为值得注意——它只大写第一个字符绝不触碰其余字符public string Transform(string input, CultureInfo culture) { if (input.Length 1) { if (char.IsUpper(input[0])) { return input; } return StringHumanizeExtensions.Concat(culture.TextInfo.ToUpper(input[0]), input.AsSpan(1)); } return culture.TextInfo.ToUpper(input); }三个关键细节首字符已是大写时直接原样返回短路优化避免无谓分配非首字符不做任何小写化所以sOME STRING.ApplyCase(LetterCasing.Sentence)的结果是SOME STRING首字符大写后的形态其余保持原状——这与Title会小写化其余字符的行为有明显区别空字符串走TextInfo.ToUpper返回空串不会崩溃。Title最复杂的智能标题转换src/Humanizer/Transformer/ToTitleCase.cs 是四个转换器中最具 Humanizer 特色的实现它包含两条转换路径与三个智能规则1ASCII 快速路径TryTransformAscii当输入全部为 ASCII 字符时走手写的逐字符扫描逻辑避免正则开销一旦遇到非 ASCII 字符current \u007F立即回退到正则路径。2正则路径TransformWithRegex使用词模式(\w|[^\u0000-\u007F])?\w*匹配单词。在 .NET 7 上该正则通过[GeneratedRegex]源生成器编译见代码中#if NET7_0_OR_GREATER分支旧框架则使用RegexOptions.Compiled预编译。3三个智能规则全大写单词保持不变如果单词本身就是全大写如NASA、API转换器跳过它不做任何改动避免把专有名词小写化连字符后的字母视为词内部分扫描逻辑中遇到会继续向后吞并字母如dont、oclock中的后续字母不会把当成单词边界短虚词在非句首位置保持小写IsArticleOrConjunctionOrPreposition维护了一个固定清单——冠词、连词、介词a、an、as、at、by、if、in、of、on、or、so、to、up、and、but、for、nor、off、the、via、yet。当这些词出现在句子中间wordIndex 0时不会首字母大写从而得到更符合英文排版习惯的标题例如The Lord of the Rings中的of、the保持小写。4土耳其语 / 阿塞拜疆语特例UsesCultureSensitiveAsciiCasing会对tr、az开头的文化启用TextInfo的大小写映射。这是为了正确处理土耳其语中无点的ı/ 有点的İ等特殊字母转换避免 ASCII 快速路径产生错误结果。与字符串 Humanize 的组合Humanize(LetterCasing)LetterCasing最常见的实战场景是作为 src/Humanizer/StringHumanizeExtensions.cs 中Humanize重载的第二个参数——把拆分单词与大小写调整一步完成public static string Humanize(this string input, LetterCasing casing) input .Humanize() .ApplyCase(casing);从源码注释与测试可见其典型输出PascalCaseInputString.Humanize(LetterCasing.AllCaps)→PASCAL CASE INPUT STRINGPascalCaseInputString.Humanize(LetterCasing.LowerCase)→pascal case input stringPascalCaseInputString.Humanize(LetterCasing.Title)→Pascal Case Input String这条重载本质上是Humanize()把驼峰/下划线拆成空格分隔的单词与ApplyCase的便捷组合开发者无需先手动拆分再手动调用ApplyCase。在枚举人性化中的应用LetterCasing同样贯穿枚举人性化 API。在 src/Humanizer/EnumHumanizeExtensions.cs 中public static string Humanize(this Enum input, LetterCasing casing) input.Humanize().ApplyCase(casing);文档注释中的示例UserType.AnonymousUser.Humanize(LetterCasing.AllCaps)→ANONYMOUS USERUserType.AnonymousUser.Humanize(LetterCasing.Title)→Anonymous UserUserType.AnonymousUser.Humanize(LetterCasing.LowerCase)→anonymous user此外还有带EnumHumanizeSource参数的重载Humanize(LetterCasing, EnumHumanizeSource)支持指定从枚举名还是从Display/Description特性取词源。位标志枚举[Flags]场景下同样可用——tests/Humanizer.Tests/BitFieldEnumHumanizeTests.cs 中composite.Humanize(LetterCasing.Title)输出SpaceX and Name Derived说明组合枚举成员名也会经过Title风格整理。在 Inflector 中的内部使用LetterCasing.Title还被 src/Humanizer/InflectorExtensions.cs 内部使用用于将去下划线、去短横线后的结果统一应用标题式大小写return humanized.Length 0 ? input : humanized.ApplyCase(LetterCasing.Title);这说明LetterCasing不只是面向外部调用者的公开 API也是 Humanizer 内部各模块之间传递大小写意图的标准媒介。测试验证与行为保证Humanizer 为四种大小写风格都建立了针对性的测试矩阵tests/Humanizer.Tests/CasingTests.cs 分别针对ApplyCaseTitle、ApplyCaseLower、ApplyCaseSentence、ApplyCaseAllCaps四个方向以理论数据[Theory]形式验证输入/输出映射tests/Humanizer.Tests/EnumHumanizeTests.cs 将四个枚举值全部纳入[InlineData(LetterCasing.Title)]等用例覆盖枚举人性化与大小写组合的每一种取值tests/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.Approve_Public_Api.DotNet8_0.verified.txt 等 API 快照文件确认public static string ApplyCase(this string input, Humanizer.LetterCasing casing)是公开 API 契约的一部分Net4.8、DotNet8/10/11 各目标框架的快照中均有对应条目任何签名变更都会触发 API 审批测试失败。使用建议与注意事项按需选择Sentence与TitleSentence只改首字符且不触碰其余文本适合保留用户原本大小写的场景Title会小写化单词其余部分并处理虚词适合生成规范化标题但对专有名词较多的文本需留意其全大写跳过规则。文化感知是默认行为四个转换器默认使用CultureInfo.CurrentCulture对应Transform(input)无文化重载需要固定输出时可显式传入CultureInfo调用To.TitleCase.Transform(input, culture)这一层。在 Humanize 流水线中组合使用处理驼峰标识符时优先使用someIdentifier.Humanize(LetterCasing.Title)而非手动拼接可避免重复编写拆分逻辑处理枚举展示文本时同理使用Enum.Humanize(LetterCasing.Sentence)等重载。不要传入未定义值任何超出03范围的枚举值都会触发ArgumentOutOfRangeException调用方应确保枚举值来自可信来源。小结LetterCasing以四个取值覆盖了字符串输出最常用的四种大小写风格并通过ApplyCase、字符串Humanize、枚举Humanize三条公开链路贯穿 Humanizer 的核心场景。其底层转换器具备文化感知、ASCII 快速路径、全大写跳过、短虚词处理等实现细节配合完整的测试矩阵与 API 快照使大小写转换这一看似简单的需求具备了可预测、可验证的工程品质。若需在项目中使用直接引入 Humanizer 包即可通过ApplyCase或Humanize(LetterCasing)获得上述全部能力。赞分享开发工具【免费下载链接】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 的 LetterCasing 枚举详解Title、AllCaps、LowerCase 与 Sentence 四种字符串大小写转换Humanizer 的 LetterCasing 枚举详解Title、AllCaps、LowerCase 与 Sentence 四种字符串大小写转换 导读 L开发工具Windows安卓子系统终极指南WSABuilds完整安装与优化教程Windows安卓子系统终极指南WSABuilds完整安装与优化教程 WSABuilds是一个强大的开源项目为Windows 10和Windows 11用户开发工具Humanizer 字符串大小写转换详解ApplyCase 方法与 LetterCasing 枚举全解析Humanizer 字符串大小写转换详解ApplyCase 方法与 LetterCasing 枚举全解析 本文围绕 Humanizer 中 CasingExt开发工具上一篇Armbian财务管理策略IT成本管理策略下一篇Obsidian Modular CSS Layout常见问题解答新手必看创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
02
RELATED NEWS

相关资讯

更多网站建设与数字化升级内容

03
WHY YAOTU

想打造同款高转化官网?

懂行业、懂生意,从建站到增长一站式陪跑

◈

场景化定制

不做模板站,围绕你的业务场景量身设计,小众不撞款。

◐

营销型架构

以转化目标组织内容与路径,让官网真正带来询盘。

▲

全周期服务

设计、开发、运营、运维一体,上线只是开始。

免费获取你的建站方案

留下需求,专属顾问 24 小时内为你输出方案建议。