字符串的第一个字符可以通过以下操作检索吗?
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]。这使用了字符串。识字课索引器。
我认为你正在寻找这个MyString.ToCharArray()[0]
:)
但是你也可以使用MyString[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";
}
}
你可以使用LINQ
char c = mystring.FirstOrDefault()
如果字符串为空,它将等于'\0'。
从字符串中获取字符可能取决于编码(字符串默认为UTF-16)
https://stackoverflow.com/a/32141891
string str = new String(new char[] { '\uD800', '\uDC00', 'z' });
string first = str.Substring(0, char.IsHighSurrogate(str[0]) ? 2 : 1);