根据字符格式化字符串

Format string based on characters

我想格式化我的字符串,在每个大写字母前加上一个 space,后面跟着一个小字符。

目前,我正在使用这个

public static string FormatCaseConvention(string text)
{
    var formatted = String.Empty;
    foreach (char letter in text)
    {
        if (Char.IsUpper(letter) && formatted.Length > 0)
        {
            formatted += " " + letter;
        }
        else
        {
            formatted += letter;
        }
    }

    return formatted;
}

但是如果输入类似于 "SQLData",它 return 是 S Q L 数据。我希望我的格式化程序 return "SQL Data"。谢谢

这是一个 RegEx 方法

string Input = "SQLData";
string Result = Regex.Replace(Input, "[A-Z][a-z]", " [=10=]");  //SQL Data

表达式:[A-Z][a-z]大写字母后跟小写字母

AFTER UPDATE

只需用这个更改您的代码即可,它运行完美。

你也可以测试一下here on fiddler

public static string FormatCaseConvention(string text)
{
    string text = "SQLDataABCHumaAdADScVascASCasASCasASCTumEKa";
    var formatted = String.Empty;
    int i = 0;
    var totalLeangth = text.Length;
    foreach (char letter in text)
    {
        if (Char.IsUpper(letter) && i < totalLeangth - 1)
        {
            if (char.IsLower(text[i + 1]) && char.IsLower(text[i - 1]))
                formatted += letter;

            else if (char.IsLower(text[i + 1]))
                formatted += " " + letter;

            else
                formatted += letter;
        }
        else if (i == totalLeangth - 1)
        {
            if (Char.IsUpper(letter) && char.IsUpper(text[i - 1]))
                formatted += letter;

            else if(Char.IsUpper(text[i-1]))                
                formatted += letter;                

            else                
                formatted += " " + letter;              
        }
        else if (Char.IsLower(letter) && char.IsUpper(text[i + 1]))
        {
            formatted += letter + " ";
        }
        else
        {
            formatted += letter;
        }
        i = i + 1;
    }
}

如果您有类似 DataSQLData 的情况,这也适用

String text  = "DataSQLData";
Regex reg = new Regex(@"[A-Z][a-z]+(?=\w*)");
reg.Replace(text, " [=10=] ").Trim().Dump();
<Extension()>
Public Function TurnIntoWords(Str As String, Optional Separator As String = " ") As String
  Dim Res As New StringBuilder(If(Str.Length, Str(0), "")), Ch As Char

  For Q As Integer = 1 To Str.Length - 2
    Ch = Str(Q)
    If _
      Char.IsUpper(Ch) _
      AndAlso (Char.IsLower(Str(Q - 1)) OrElse Char.IsLower(Str(Q + 1))) _
    Then
      Res.Append(Separator)
    End If

    Res.Append(Ch)
  Next Q

  If Str.Length Then Res.Append(Str(Str.Length - 1))

  Return Res.ToString()
End Function
public static string TurnIntoWords(this string Str, string Separator = " ")
{
    StringBuilder Res = new StringBuilder(Str.Length != 0 ? Str[0].ToString() : "");
    char Ch = '[=11=]';

    for (int Q = 1; Q <= Str.Length - 2; Q++)
    {
        Ch = Str[Q];

        if (char.IsUpper(Ch) && (char.IsLower(Str[Q - 1]) || char.IsLower(Str[Q + 1])))
            Res.Append(Separator);

        Res.Append(Ch);
    }

    if (Str.Length != 0)
        Res.Append(Str[Str.Length - 1]);

    return Res.ToString();
}