加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 用带连字符的字符串替换camel case字符串

发布时间:2020-12-15 08:22:37 所属栏目:百科 来源:网络整理
导读:我该如何更改字符串 aboutUs 至 about-us 如果可能的话,我希望能够使用正则表达式替换. 我试过了: public static string ToHypenCase(this string source) { return Regex.Replace(source,@"[A-Z]","-$1");} 解决方法 您可以使用正则表达式和ToLower()的组
我该如何更改字符串

aboutUs

about-us

如果可能的话,我希望能够使用正则表达式替换.
我试过了:

public static string ToHypenCase(this string source) {
    return Regex.Replace(source,@"[A-Z]","-$1");
}

解决方法

您可以使用正则表达式和ToLower()的组合来执行此操作,如下所示:
string s = "quickBrownFoxJumpsOverTheLazyDog";
string res = Regex.Replace(s,@"([a-z])([A-Z])","$1-$2").ToLower();
Console.WriteLine(res);

Demo on ideone.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读