C# 如何将文本块划分为每个字符,以便能够逐个分析字符?
早上好,我想要一些关于WPF/C#(Visual Studio)的帮助: 我有一个文本框,用户必须在其中编写程序必须分析的代码。必须逐个字符分析此代码,因为每个字符都有不同的含义。如何将文本框的文本划分为每个字符,然后使用if进行分析?谢谢按照步骤操作C# 如何将文本块划分为每个字符,以便能够逐个分析字符?,c#,wpf,visual-studio,code-behind,textblock,C#,Wpf,Visual Studio,Code Behind,Textblock,早上好,我想要一些关于WPF/C#(Visual Studio)的帮助: 我有一个文本框,用户必须在其中编写程序必须分析的代码。必须逐个字符分析此代码,因为每个字符都有不同的含义。如何将文本框的文本划分为每个字符,然后使用if进行分析?谢谢按照步骤操作 假设您想阅读在按钮上的文本框中输入的文本,请点击代码隐藏(我现在不支持MVVM或其他模式) 在xaml中添加以下代码 <TextBox x:Name="MessageTextBox" /> <Bu
<TextBox x:Name="MessageTextBox" />
<Button Content="Home" Click="Button_Click"/>
请尝试使用上述语句并进行检查。字符串基本上是字符数组。因此,您可以使用索引访问器(
mystring[2]
)、for
和foreach
循环何时进行分析:输入字符时?当按钮被点击时?在其他事件上?可能重复
private void Button_Click(object sender, RoutedEventArgs e)
{
string textBoxValue = MessageTextBox.Text;
if (string.IsNullOrWhiteSpace(textBoxValue))
{
// Display an error or warning message to ask user to enter some text.
return;
}
else
{
foreach(char ch in textBoxValue)
{
// Now you have each character of the text entered in the textbox
// You can write your logic now.
}
}
}