字符串的第一个字符可以通过以下操作检索吗?
MyString.ToCharArray[0]
字符串的第一个字符可以通过以下操作检索吗?
MyString.ToCharArray[0]
当前回答
也许这个会有帮助。我使用txtModel_Leave事件,然后创建方法来检测主文本框中的第一个字符。
private void txtMain_Leave(object sender, EventArgs e)
{
detectFirstChar();
}
private void detectFirstChar()
{
string mystr = txtModel.Text;
if (String.IsNullOrEmpty(txtModel.Text))
{
txtAwalan.Text = "";
}
else if (mystr.Substring(0, 1) == "F")
{
txtKategori.Text = "Finishing";
}
else if((mystr.Substring(0, 1) == "H"))
{
txtKategori.Text = "Half";
}
else
{
txtKategori.Text = "NONE";
}
}
其他回答
只是MyString[0]。这使用了字符串。识字课索引器。
你可以使用LINQ
char c = mystring.FirstOrDefault()
如果字符串为空,它将等于'\0'。
下面从字符串中获取第一个字符的示例可能会对某些人有所帮助
string anyNameForString = "" + stringVariableName[0];
MyString。删除(1、2);同样适用
也许这个会有帮助。我使用txtModel_Leave事件,然后创建方法来检测主文本框中的第一个字符。
private void txtMain_Leave(object sender, EventArgs e)
{
detectFirstChar();
}
private void detectFirstChar()
{
string mystr = txtModel.Text;
if (String.IsNullOrEmpty(txtModel.Text))
{
txtAwalan.Text = "";
}
else if (mystr.Substring(0, 1) == "F")
{
txtKategori.Text = "Finishing";
}
else if((mystr.Substring(0, 1) == "H"))
{
txtKategori.Text = "Half";
}
else
{
txtKategori.Text = "NONE";
}
}