C#中,怎么提取一个字符串中的指定字符比如str = "ABCDE"
我要提取 B这个字符的位置
不能这样
str.substring(1,1);
不能这样,因为我不看这个字符串
我要在不知道这个字符串中有没有B字符的情况下,判断其位置,请教!
我要提取 B这个字符的位置
不能这样
str.substring(1,1);
不能这样,因为我不看这个字符串
我要在不知道这个字符串中有没有B字符的情况下,判断其位置,请教!
如果返回的是-1,说明字符串里没有你要找的字符。
可以用for循环来找你想要的字符,要是没有就返回相关信息。
也可以用contians方法来查找,都很简单。
string str = "123456";
bool s = str.Contains("2");
Console.WriteLine(s);
Console.ReadKey();
结果返回true就是找到你要的字符,false就没有。
string mybalance = "12234";
if (mybalance.Contains("B"))
{
return mybalance.Substring(mybalance.IndexOf("B"), 1);
}
return -1;
class Program
{
static void Main(string[] args)
{
string str = "123456";
bool s = str.Contains("2");
if (s)
{
Console.WriteLine(str.IndexOf("4")+1);
}
Console.ReadKey();
}
}
//存在
else
//不存在