【问题标题】:Insert space before number in PascalCase string在 PascalCase 字符串中的数字前插入空格
【发布时间】:2013-02-03 21:55:23
【问题描述】:

我想在数字前放置一个空格。

例如,我有这个 PascalCase 字符串:“SupportContactAddressLine1”。

我希望它显示“支持联系人地址行 1”

我试过这个:

var s = PascalCase;

 for (var i = 1; i < s.Length; i++)
 {
  if (char.IsLower(s[i - 1]) && char.IsUpper(s[i]))
  {
    s = s.Insert(i, " ");
  }
 }

但结果是:“Support Contact Address Line1”

【问题讨论】:

    标签: c# string space


    【解决方案1】:

    也检查一个数字:

    if (Char.IsLower(s[i - 1]) && (Char.IsUpper(s[i]) || Char.IsDigit(s[i])))
    

    【讨论】:

      【解决方案2】:

      数字不是字母,因此没有大小写之分,char.IsUpper('1') 返回false

      您应该使用char.IsDigit(...) 来检查数字。

      【讨论】:

        【解决方案3】:

        这应该对你有帮助:

        var res = Regex.Replace("SupportContactAddressLine100", "([A-Z])|([0-9]+)", " $1$2");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-10-22
          • 2014-03-31
          • 2015-07-06
          • 2012-08-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-24
          相关资源
          最近更新 更多