根据字符格式化字符串

时间:2016-02-11 09:29:50

标签: c# string space formatter

我想格式化我的字符串,以便在每个大写字母前跟一个小字符创建一个空格。

目前,我正在使用此

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”,则返回S Q L Data。我希望我的格式化程序返回“SQL数据”。感谢。

4 个答案:

答案 0 :(得分:4)

这是RegEx方法

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

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

答案 1 :(得分:2)

  

更新后

只需使用此代码更改您的代码,它就是完美的工作。

您还可以对其进行测试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;
    }
}

答案 2 :(得分:0)

这也适用于像DataSQLData

这样的情况
String text  = "DataSQLData";
Regex reg = new Regex(@"[A-Z][a-z]+(?=\w*)");
reg.Replace(text, " $0 ").Trim().Dump();

答案 3 :(得分:0)

<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 = '\0';

    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();
}