C# 实现大写字母拆分单词算法

以下代码实现了基于大写字母智能拆分单词的功能。
例如文本为"ILoveYou.",拆分后得到结果"I Love You."。

public static string SplitWord (string text)
{
try
{
StringBuilder result = new StringBuilder();
result.Append(text[0]);
for (int i = 1; i < text.Length - 1; i++)
{
if ((Char.IsUpper(text[i]) && (Char.IsLower(text[i + 1])) || Char.IsUpper(text[i]) && Char.IsLower(text[i - 1])))
{
result.Append($" {text[i].ToString()}");
}
else
{
result.Append(text[i]);
}
}
result.Append(text[text.Length - 1]);
return (result.ToString());
}
catch (Exception ex)
{
return (text);
}
}
 

猜你喜欢

转载自blog.csdn.net/zcr_59186/article/details/128633570
今日推荐