如何获取任何字符串的第一个字符 [英] how to get first character of any string

查看:733
本文介绍了如何获取任何字符串的第一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).


这篇关于如何获取任何字符串的第一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆