字符串截取问题 http://www.xxxx.com/aaaaa/http://www.xxxx.com.cn/aaaaa/www.xxxx.com/aaaaa/www.xxxx.com.cn/aaaaa/以上的四种情况,我只想取得"aaaaa"这部份怎么写一个通用的截取方法~! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 string str = "http://www.xxxx.com/aaaaa/"; //还可以是"http://www.xxxx.com.cn/aaaaa/","www.xxxx.com/aaaaa/"等string resultStr = str.Substring(str.Length - 6, 5); //结果 public string getUrl(str){string newstring = str.Substring(str.IndexOf('/')).Remove('/');return newstring;} String str = "http://www.xxxx.com/aaaaa/"; Int32 index1 = str.IndexOf("com/"); Int32 index2 = str.IndexOf("cn/"); Int32 index = (index1 > index2? index1:index2); index1 = str.IndexOf('/', index); index2 = str.IndexOf('/', index1+1); String res = str.Substring(index1+1,index2-index1-1); Console.WriteLine(res); string a = "http://www.xxxx.com/aaaaa/"; string tmp = a.Remove(a.LastIndexOf('/')); MessageBox.Show(tmp.Substring(tmp.LastIndexOf('/')+1)); string test = "http://www.xxxx.com/aaaaa/"; string result = test.Substring(test.Length - 6, 5);6是倒数第二个/之后的长度,5是aaaaa的长度 6和5是可变的,试试;根据实际情况而定 貌似浏览器的自动加http过程 反过来首先判断是不是http://开头是的话去掉http://然后查找剩下字符的从"/"开始到结尾的字符(相对路径吧)如果只要一级,那就要第一个 "/"到第二个"/"之间即可。 str = str.ToLower().Replace("http://",""); str = str.Substring(str.IndexOf("/")+1, str.LastIndexOf("/") - str.IndexOf("/")-1);也可以使用正则 string w = str.Substring(str.Length - 6, 5); Console.WriteLine(w); 3楼的,Remove不可以那样那吧~!他只重写好两种情况,一是指定索引的删除,二是指定索引,长度的删除~!2楼的,aaaaa只是个比喻,长度我也不知道~!所以用长度做文章好像也不行吧!~沙发高歌,我也知道是取最后两个//中间的内容,不会才请教大家啊!~ 正则表达式:@"(?<=com\S*?/)\S+(?=/)"另外lz需要说明可能的变化才能写出最佳匹配的表达式.否则只能根据楼主的字面进行匹配了. 随机密码插入数据库。为什么会有很多密码一样。。 <asp:image 宽度的设置 DataGridView中怎样得到选定行某一制定列值 求教,如何给定义中的第几个星期,获得其日期范围 非得用System.Data.DataRow吗? 问几个问题,非常急,在线等 想下个VisulaStudio.NET 2003 如何得到记录数? 带小数,如何只取整数位 C#初学者求助大神 C#异步调用的困惑 跪求 c# 多线程
string resultStr = str.Substring(str.Length - 6, 5); //结果
{
string newstring = str.Substring(str.IndexOf('/')).Remove('/');
return newstring;
}
Int32 index2 = str.IndexOf("cn/"); Int32 index = (index1 > index2? index1:index2); index1 = str.IndexOf('/', index);
index2 = str.IndexOf('/', index1+1); String res = str.Substring(index1+1,index2-index1-1);
Console.WriteLine(res);
string a = "http://www.xxxx.com/aaaaa/";
string tmp = a.Remove(a.LastIndexOf('/'));
MessageBox.Show(tmp.Substring(tmp.LastIndexOf('/')+1));
string result = test.Substring(test.Length - 6, 5);
6是倒数第二个/之后的长度,5是aaaaa的长度 6和5是可变的,试试;根据实际情况而定
str = str.Substring(str.IndexOf("/")+1, str.LastIndexOf("/") - str.IndexOf("/")-1);也可以使用正则
Console.WriteLine(w);
@"(?<=com\S*?/)\S+(?=/)"
另外lz需要说明可能的变化才能写出最佳匹配的表达式.否则只能根据楼主的字面进行匹配了.