从右到左判断字符串 C#

来源:百度知道 编辑:UC知道 时间:2024/05/15 15:03:20
string mystring="http://123.com/123.shtml";

我需要实现这样的效果

从右到左搜索6个字符,如果是.shtml,成功,如果不是,failed

mystring.EndsWith(".shtml");

EndsWith是一个检查字符串是不是以指定的字符串结尾的方法(bool)

首先判断你的字符串mystring长度是否大于6.写程序要严谨。
if(mystring.Length>6)
{
string resultString=mystring.Substring(mystring.Length-6,6);
再判断resultString.
if(resultString==".shtml")
{
成功
}
else
{
失败
}
}
else
{
失败
}

string str=mystring.SubString(mystring.LastIndexOf(".")+1);
if(str=="shtml")
return true;
else
return false;

string result=mystring.Substring((int i=mystring.Length)-7,6);
if(i>6){
if(resultString=="shtml")
{
MessageBox.Show("成功");}
else
{
MessageBox.Show("失败");
}
} else{MessageBox.Show("少于6位失败");
}

用.EndsWith()方法更好一些吧。