字符串替换
string Replace(string oldValue,string newValue)
将字符串中出现的oldValue的地方替换为newValue
举例:
string str = "明天又是美好的一天";
str = str.Replace('又', '不');//将oldValue的地方替换为newValue
Console.WriteLine(str);
Console.ReadKey();
截取子字符串
string Substring(int startIndex)截取从位置startIndex开始一直到最后的字符串
举例:
string str = "明天又是美好的一天";
str = str.Substring(4);//截取从位置startIndex开始一直到最后的字符串
Console.WriteLine(str);
Console.ReadKey();
string Substring(int startIndex,int length)截取从位置startIndex开始长度为length的子字符串
注意:如果字符串的长度不足length则报错
string str = "明天又是美好的一天";
str = str.Substring(4,5);//截取从位置startIndex开始长度为length的子字符串
Console.WriteLine(str);
Console.ReadKey();
字符串位置
int IndexOf(string value)
找子串value的第一次出现的位置
举例:
string str = "明天又是美好的一天";
int index = str.IndexOf("好");//子串第一次出现的位置
//如果找到了,就把索引显示出来
//如果找不到,就返回-1
Console.WriteLine(index);
Console.ReadKey();
int IndewOf(string value,int startIndex)
字符串中,从某个索引开始找子串出现的位置
string str = "明天又是美好的一天";
int index = str.IndexOf("好",0);//从某个索引开始找子串出现的位置
//如果找到了,就把索引显示出来
//如果找不到,就返回-1
Console.WriteLine(index);
Console.ReadKey();
判断字符串
判断字符串中是否含有子字符串value
bool Contains(string value)
举例:
string str = "明天又是美好的一天";
bool result = str.Contains("美好");//判断字符串中是否含有子字符串"美好"
if(result)
{
Console.WriteLine("不存在您查询的子字符串");
}
else
{
Console.WriteLine("存在您查询的子字符串");
}
Console.ReadKey();
判断字符串是否以子串value开始
bool StartsWith(string value)
举例:
string str = "明天又是美好的一天";
bool result = str.StartsWith("明");//判断字符串是否以某个字符串开始
if (result)
{
Console.WriteLine("存在该字符串");
}
else
{
Console.WriteLine("不存在该字符串");
}
Console.ReadKey();
判断字符串是否以子串value结束
bool EndsWith(string value)
举例:
string str = "明天又是美好的一天";
bool result = str.StartsWith("一天");//判断字符串是否以某个字符串结束
if (result)
{
Console.WriteLine("存在该字符串");
}
else
{
Console.WriteLine("不存在该字符串");
}
Console.ReadKey();