字符串的第一个字符可以通过以下操作检索吗?
MyString.ToCharArray[0]
字符串的第一个字符可以通过以下操作检索吗?
MyString.ToCharArray[0]
当前回答
我认为你正在寻找这个MyString.ToCharArray()[0]
:)
但是你也可以使用MyString[0]。
其他回答
或者你可以这样做:
MyString[0];
这是另一种方法:
string mystr = "hello";
MessageBox.show(mystr.Substring(0, 1));
试试这个,
字符串s1 =“好”; 字符串s = s1.Substring (0,1);
从字符串中获取字符可能取决于编码(字符串默认为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);
也许这个会有帮助。我使用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";
}
}