如何在一组特定字符中的每一个之后使字母大写 [英] How do I make letters to uppercase after each of a set of specific characters
本文介绍了如何在一组特定字符中的每一个之后使字母大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符集合(',','。','/','-',''),然后有一个字符串集合(约500个)。
I have a collection of characters (',', '.', '/', '-', ' ') then I have a collection of strings (about 500).
我想尽可能快地做的是:在每个字符之后,我想将下一个字母大写。
What I want to do as fast as possible is: after each of the characters I want to make the next letter uppercase.
我也希望首字母大写,并且许多字符串都以大写开头。
I want the first capitalized as well and many of the strings are all uppercase to begin with.
编辑:
我修改了tdragons对最终结果的答案:
I modified tdragons answer to this final result:
public static String CapitalizeAndStuff(string startingString)
{
startingString = startingString.ToLower();
char[] chars = new[] { '-', ',', '/', ' ', '.'};
StringBuilder result = new StringBuilder(startingString.Length);
bool makeUpper = true;
foreach (var c in startingString)
{
if (makeUpper)
{
result.Append(Char.ToUpper(c));
makeUpper = false;
}
else
{
result.Append(c);
}
if (chars.Contains(c))
{
makeUpper = true;
}
}
return result.ToString();
}
然后我为所有字符串调用此方法。
Then I call this method for all my strings.
推荐答案
string a = "fef-aw-fase-fes-fes,fes-,fse--sgr";
char[] chars = new[] { '-', ',' };
StringBuilder result = new StringBuilder(a.Length);
bool makeUpper = true;
foreach (var c in a)
{
if (makeUpper)
{
result.Append(Char.ToUpper(c));
makeUpper = false;
}
else
{
result.Append(c);
}
if (chars.Contains(c))
{
makeUpper = true;
}
}
这篇关于如何在一组特定字符中的每一个之后使字母大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文