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

本文关键字:HelloWord Hello World 例如 之间 字符串 添加 空格 | 更新日期: 2023-09-27 18:01:25

假设我有几个字符串:CustomerName, CustomerID, ContactNumber,…如何将这些字符串转换为客户名称、客户ID、联系电话?在c#中是否有任何内置的方法可以用来实现这一点?还是我一个一个手工做?

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

除了基于regex的方法(参见解决方案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

方案二 (regex):

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

您可以使用以下扩展方法:

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

我假设CustomerName, CustomerID是属性名称?您将需要一些方法来反映这些名称,然后您可以使用Humanizer Lib来添加空白。

像这样使用:

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