在字符串之间添加空格(例如HelloWord - > Hello World)

时间:2015-07-07 08:51:05

标签: c# string whitespace

假设我有一些字符串:CustomerName,CustomerID,ContactNumber,...如何将这些字符串转换为客户名称,客户ID,联系号码?在c#中是否可以使用任何内置方法来实现?或者我应该手动逐一进行吗?

3 个答案:

答案 0 :(得分:3)

除了基于正则表达式的方法之外,没有内置的方法可以实现您的目标(参见解决方案2)。

解决方案1 ​​(无正则表达式,自定义方法):

static string SeparateTitleCases(this string source, string separator = " ")
{
    var result = new StringBuilder();
    char previousChar = default(char);

    for (int i = 0; i < source.Length; i++)
    {
        char currentChar = source[i];

        if (char.IsLower(previousChar) && // Previous char is lowercase
            char.IsUpper(currentChar)) // Current char is uppercase
        {
            result.Append(separator); // Append separator
        }
        result.Append(currentChar);

        previousChar = currentChar;
    }

    return result.ToString();
}

样本用法:

Console.WriteLine("CustomerName".SeparateTitleCases()); // Customer Name
Console.WriteLine("CustomerID".SeparateTitleCases()); // Customer ID
Console.WriteLine("CustomerName CustomerID".SeparateTitleCases()); // Customer Name Customer ID

解决方案2 (正则表达式):

string pattern = @"([^\s])([A-Z]+[a-z]*)";
string replacement = "$1 $2";            

Console.WriteLine(Regex.Replace("CustomerName", pattern, replacement)); // Customer Name
Console.WriteLine(Regex.Replace("CustomerID", pattern, replacement)); // Customer ID
Console.WriteLine(Regex.Replace("CustomerName CustomerID", pattern, replacement)); // Customer Name Customer ID

答案 1 :(得分:1)

您可以使用此扩展方法:

private static readonly HashSet<UnicodeCategory> SeparatorCharCategories = new HashSet<UnicodeCategory>{ UnicodeCategory.UppercaseLetter, UnicodeCategory.TitlecaseLetter, UnicodeCategory.DecimalDigitNumber };

public static String SeparateCharCategories(this string input, string delimiter = " ")
{
    StringBuilder sb = new StringBuilder(input.Length);
    UnicodeCategory lastCharCategory = Char.GetUnicodeCategory(input[0]);
    for(int i = 0; i < input.Length; i++)
    {
        UnicodeCategory charCategory = Char.GetUnicodeCategory(input[i]);
        if (lastCharCategory != charCategory && SeparatorCharCategories.Contains(charCategory))
            sb.Append(delimiter);
        sb.Append(input[i]);
        lastCharCategory = charCategory;
    }
    return sb.ToString();
}

您的样品和其他边缘情况:

var items = new[] { "CustomerName", "CustomerID", "ContactNumber", "PurchaseOrders", "purchaseOrders", "The2Unlimited", "Unlimited2", "222Unlimited", "The222Unlimited", "Unlimited222", "ATeam", "TheATeam", "TeamA", "HTMLGuide", "TheHTMLGuide", "TheGuideToHTML", "HTMLGuide5", "TheHTML5Guide", "TheGuideToHTML5", "TheUKAllStars", "AllStarsUK", "UKAllStars" };
foreach (string str in items)
    Console.WriteLine(str.SeparateCharCategories(" "));

您看到不支持首字母缩略词,因此HTMLGuide仍为HTMLGuide

Customer Name
Customer ID
Contact Number
Purchase Orders
purchase Orders
The 2 Unlimited
Unlimited 2
222 Unlimited
The 222 Unlimited
Unlimited 222
ATeam
The ATeam
Team A
HTMLGuide
The HTMLGuide
The Guide To HTML
HTMLGuide 5
The HTML 5 Guide
The Guide To HTML 5
The UKAll Stars
All Stars UK
UKAll Stars

答案 2 :(得分:0)

我假设CustomerNameCustomerID是属性名称?您需要某种方式来反映这些名称,然后您可以使用Humanizer Lib添加空格。

像这样使用:

"CustomerName".Humanize(LetterCasing.Title) => "Customer Name";