gpt4 book ai didi

c# - 分词应用

转载 作者:太空宇宙 更新时间:2023-11-03 12:21:38 25 4
gpt4 key购买 nike

此应用程序应该采用 Pascal 大小写的字符串:

HelloHowAreYou?

然后将单词分开形成一个句子,只保留第一个字母大写:

你好,你好吗?

截至目前,此代码仅在每个单词以不同字母作为星号时才有效。例句 HelloHowAreYou 结果是 hellohow are you?

为什么要这样做?

    private void btnSeparate_Click(object sender, EventArgs e)
{
// Get the sentence from the text box
string sentence = txtWords.Text;
int upperCase; // to hold the index of an uppercase letter

foreach (char up in sentence)
{

if (char.IsUpper(up))
{

// Find the index of the uppercase letter
upperCase = sentence.IndexOf(up);

// Insert a space at the appropriate index
sentence = sentence.Insert(upperCase, " ");
}
}
// Make all the letters lowercase
sentence = sentence.ToLower();
// Capitalize the first letter of the sentence.
sentence = sentence[1].ToString().ToUpper() + sentence.Substring(2);

// Display the separeted words
lblSeparatedWords.Text = sentence;

}
}

最佳答案

这对你有用吗:

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


在您的示例中,HHelloHow 中重复,您没有获得所需的输出。

你可以从我上面的解决方案中创建一个方法:

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

关于c# - 分词应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46945725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com