这个应用程序应该写一个字符串写的是pascal case:
for p in (row):
column_num = 1
_= ws0.cell(row = row_number,column= column_num,value = (p.column_num))
并将单词分开形成一个句子,只保留首字母大写:
HelloHowAreYou?
截至目前,此代码仅在每个单词用不同的字母标出时才有效。示例句Hello how are you?
以HelloHowAreYou
为什么要这样做?
hellohow are you?
答案 0 :(得分:1)
这对你有用吗?
var test = "HelloHowAreYou";
var final = "";
bool firstCharacterCheckIsDone = false;
foreach (char c in test)
{
if (char.IsUpper(c))
{
if (test.IndexOf(c) == 0 && !firstCharacterCheckIsDone)
{
final += " " + c.ToString();
firstCharacterCheckIsDone = true;
}
else
final += " " + c.ToString().ToLower();
}
else
final += c.ToString();
}
Console.WriteLine(final.Trim());
输出:
Hello how are you
检查Fiddle
正如您的示例中H
正在重复Hello
& How
你没有得到你想要的输出。
您可以使用我的上述解决方案制作方法:
public static void Main()
{
Console.WriteLine(FinalOutput("HelloHowAreYou?"));
}
static string FinalOutput(string test)
{
var final = "";
bool firstCharacterCheckIsDone = false;
foreach (char c in test)
{
if (char.IsUpper(c))
{
if (test.IndexOf(c) == 0 && !firstCharacterCheckIsDone)
{
final += " " + c.ToString();
//This here will make sure only first character is in Upper case
//doesn't matter if the same character is being repeated elsewhere
firstCharacterCheckIsDone = true;
}
else
final += " " + c.ToString().ToLower();
}
else
final += c.ToString();
}
return final.Trim();
}
输出:
Hello how are you?
检查Fiddle
答案 1 :(得分:0)
原始版本出现相同字母问题的原因是您使用了IndexOf
。该方法只返回字母的第一个索引,因此如果您有多个以相同字母开头的单词,则您的版本只会更改第一个字母。
这是另一个选项,使用字符串构建器:
string SeparateToWords(string pascalSentence)
{
if(string.IsNullOrEmpty(pascalSentence))
{
return pascalSencentce;
}
var sb = new StringBuilder();
// note I'm starting from 1 not from 0 here
for(var i = 1; i < pascalSentence.Length; i++)
{
if(char.IsUpper(pascalSentence[i]))
{
sb.Append(" ").Append(pascalSentence[i].ToString().ToLower());
}
else
{
sb.Append(pascalSentence[i]);
}
}
sb.Insert(0, pascalSentence[0]);
return sb.ToString();
}