如何获取任何字符串的第一个字符

问题描述:

你好朋友
这是我的代码...

hello friend
here is my code...

string l = dataGridView1[4, a].Value.ToString();
                        if (l == "Opening Sauda")
                        {

                            trantype11 = "O";

                        }
                        else if (l == "Percentage Wise")
                        {

                            trantype11 = "P";

                        }
                        else if (l == "Transation")
                        {

                            trantype11 = "T";
                        }



如果datagrid视图单元格可以包含明智百分比
之类的字符串 比trentype11 sholud设置为charector p
就像

我尝试了但没有用(它给了最后一个字符e但我想要p)



if datagrid view cell cantain string like Percentage Wise
than trentype11 sholud set forst charector p
like

i try it but no working(it give last character e but i want p )

string b = dataGridView1[1, a].Value.ToString();
                        broktype11= b.Substring(1, 1);

不要使用子字符串,只需使用char first = b[0].如果由于某种奇怪的原因,您需要一个包含此字符的字符串,则为string oneChar = new string(first, 1).就这样.

—SA
Don''t use substring, use simply char first = b[0]. If by some strange reason you need a string containing this one char, it would be string oneChar = new string(first, 1). That''s it.

—SA


尝试
string b = dataGridView1[1, a].Value.ToString();
                        broktype11= b.Substring(0, 1);


如果您想要字符串中的第一个字符,请使用.Substring(0,1)-而不是.Substring(1,1).
If you want the first character in a string use .Substring(0,1) - not .Substring(1,1).