C#里如何确定一个字符在在字符串中的位置啊?

发布网友

我来回答

5个回答

热心网友

确定一个字符在字符串中的位置关键代码为:

String lstg_test ="hello world" ;int lint_index = lstg_test.IndexOf('o');

1.在字符串中获得某数字位置时,可以使用string类的IndexOf方法,该方法用来确定指定字符在字符串中的索引,如果在字符串中能找到指定字符,则返回其索引,否则返回-1。

2.在字符串中获得数字位置的关键代码如下:string str = textBox1.Text.Trim(); 
int index = str.IndexOf(textBox2.Text.Trim()); 
if (index >= 0)
MessageBox.Show("数字" + textBox2.Text + "在字符串中的位置为:" + (index+1), "信息", MessageBoxButtons.OK, MessageBoxIcon.Information); 
else
MessageBox.Show("没有要查找的数字", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

热心网友

int index=s.IndexOf("a");
index 就是a字符在字符串s 中第一次出现的索引位置
s.LastIndexOf("a") 得到a 字符在s 中倒数第一次出现的索引位置追问那假如,a在s里出现的次数不止一次,我要一一确定他们的位置该怎么办啊?

追答那样用循环好了
ArrayList lt = new ArrayList();
string str = "sfskjfskfakfjaga";
int index = 0;
foreach(Char ch in str)
{
if (ch == 's')
{
lt.Add(index);
}
index++;
}
最后得到的lt 里面存的就是你所有出现字符的索引位置

热心网友

s.IndexOf(a)好像是这个方法的,可以确定字符串的第一次出现的索引

热心网友

首次出现的位置:s.IndexOf('a')
最后一次出现的位置:s.LastIndexOf('a')

热心网友

s.IndexOf('a') > -1

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com