C#字符串查找、替换
字符串查找的方法IndexOf、LastlndexOf
IndexOf方法得到的是指定字符串在原字符串中第一次出现的位置
LastlndexOf方法得到的是指定字符串在查找的字符串中最后一次出现的位置
返回查找到字符串的位置,如未查找到返回-1
语法
- IndexOf(String value[,int startIndex][,int count]);
- LastlndexOf(String value[,int startIndex][,int count]);
String value:需查找的字符
int startIndex:开始查找的位置
int count:结束查找的位置
- string s = "hello world";
- int fIndex = s.IndexOf("w");
- if (fIndex == -1)
- {
- MessageBox.Show("没有找到字符");
- }
- else
- {
- MessageBox.Show("位置在:" + fIndex);
- }
字符串替换
- string s = "hello world";
- s.replace("h","H");
扫描二维码推送至手机访问