You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中小驼峰转大驼峰,如何排除点号的影响?

小驼峰(camelCase)转大驼峰(PascalCase)并保留点号分隔

实现方案

拆分拼接法

逻辑直观易理解,适合快速实现:

  • 按点号将输入字符串拆分为独立字段
  • 对每个字段执行首字母大写操作,其余字符保持原样
  • 用点号将处理后的字段重新拼接
using System;
using System.Linq;

public class Program
{
    public static void Main()
    {
        string input = "customer.presentAddress.streetName";
        string output = string.Join(".", 
            input.Split('.').Select(field => char.ToUpper(field[0]) + field.Substring(1)));
        Console.WriteLine(output); // 输出:Customer.PresentAddress.StreetName
    }
}

正则表达式法

更简洁的实现方式,通过正则匹配目标字符并替换:
正则表达式(^|\.)([a-z])会匹配两种场景:字符串开头的小写字母、点号后的小写字母,随后将匹配到的小写字母转为大写,保留原有分隔符或开头位置。

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        string input = "customer.presentAddress.streetName";
        string output = Regex.Replace(input, @"(^|\.)([a-z])", 
            match => match.Groups[1].Value + char.ToUpper(match.Groups[2].Value));
        Console.WriteLine(output); // 输出:Customer.PresentAddress.StreetName
    }
}

内容的提问来源于stack exchange,提问作者Prasanna Kumar J

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 19:45:42