c语言 string indexof,c语言indexof的用法有哪些

小编整理了c语言 indexof的用法。希望对你有帮助哦!

IndexOf()

查找字串中指定字符或字串首次出现的位置,返首索引值,如: str1.IndexOf("字"); //查找“字”在str1中的索引值(位置)

str1.IndexOf("字串");//查找“字串”的第一个字符在str1中的索引值(位置) str1.IndexOf("字",start,end);//从str1第start+1个字符起,查找end个字符,查找“字”在字符串STR1中的位置[从第一个字符算起]注意:start+end不能大于str1的长度

indexof参数为string,在字符串中寻找参数字符串第一次出现的位置并返回该位置。如string s="0123dfdfdf";int i=s.indexof("df");这时i==4。

如果需要更强大的字符串解析功能应该用Regex类,使用正则表达式对字符串进行匹配。

indexof() :在字符串中从前向后定位字符和字符串;所有的返回值都是指在字符串的绝对位置,如为空则为- 1

string test="asdfjsdfjgkfasdsfsgfhgjgfjgdddd";

test.indexof(’d’) =2 //从前向后定位 d 第一次出现的位置

test.indexof(’d’,5,2) =6 //从前向后定位 d 从第5 位开始查,查2位,即从第5位到第7位;

lastindexof() :在字符串中从后向前定位字符和字符串; 用法和 indexof() 完全相同。

下面介绍 IndexOfAny ||lastindexofany

他们接受字符数组做为变元,其他方法同上,返回数组中任何一个字符最早出现的下标位置 如下

char[] bbv={’s’,’c’,’b’};

string abc = "acsdfgdfgchacscdsad";

Response.Write(abc.IndexOfAny(bbv))=1 Response.Write(abc.IndexOfAny(bbv, 5))=9 Response.Write(abc.IndexOfAny(bbv, 5, 3))=9 lastindexofany 同上。

substring() 用法

string a="aadsfdjkfgklfdglfd"

a.substring(5) //截取从第五位以后的所有字符串 a.substring(0,5) //截取从第0到第5 以后的所有字符串

var script = document.createElement('script'); script.src = 'http://static.pay.baidu.com/resource/baichuan/ns.js'; document.body.appendChild(script);

C# code

privatevoid btnLog_Click(object sender, EventArgs e)

{

//登陆判断

string userName = this.texName.Text; string userpwd = this.texPwd.Text; string userCard=this.texCombo.Text;

try {

int id = Convert.ToInt32(userName);

string getpwd = Employee.SelectByID(id).Password; if (userName == getpwd && userCard != "--请选择--")

{

//登陆正确

LoginInf.userName = userName; LoginInf.userPwd = userpwd; LoginInf.userCad = userCard;

//关闭登陆框转到首页

this.Hide();

new Home().ShowDialog();

this.Close(); }

else

{

//登陆失败

MessageBox.Show("登陆失败");

var script = document.createElement('script'); script.src = 'http://static.pay.baidu.com/resource/baichuan/ns.js'; document.body.appendChild(script);

} } catch {

MessageBox.Show("登陆失败!!!");

return; }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值