求一个截取字符串的方法

maihuasen1978 2010-07-09 11:58:33
string str="http://www.baidu.com/temp/abc.aspx?id=1";
现在要得到temp后面的那个"/"往右的所有字符,也就是说得到abc.aspx?id=1
应该如何截取?
...全文
103 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanghui0380 2010-07-09
  • 打赏
  • 举报
回复
string str = "http://www.baidu.com/temp/abc.aspx?id=1";
res = System.IO.Path.GetFileName(str); ;



  Uri xx = new Uri("http://www.baidu.com/temp/abc.aspx?id=1");
string res = String.Format("{0}{1}", xx.Segments[xx.Segments.Length - 1], xx.Query);
一切为了你 2010-07-09
  • 打赏
  • 举报
回复
截取Url域名 http://focus.it168.com/focus/201006/mobile/index.html

这是一个常见的Url,如果我们要取得地址中的域名,也就是:

http://focus.it168.com

.net(C#方法实现)

web版本:

  string str = "http://focus.it168.com/focus/201006/mobile/index.html";
int len1 = str.IndexOf(':')+3;
int len = str.IndexOf('/', len1 , str.Length - len1 );
Response.Write(str.Substring(0,len));

winform版本:

  string str = "http://focus.it168.com/focus/201006/mobile/index.html";
int len1 = str.IndexOf(':')+3;
int len = str.IndexOf('/', len1 , str.Length - len1 );
Console.WriteLine(str.Substring(0,len));
  Console.Read();



输出结果:http://focus.it168.com/

其他地址也一样,大家可以把url当参数传进来

例如:

public static void Main()
{
string str="http://bj.house.163.com/10/0609/09/68NO989500073SD3.html";
RL(str);
}

private static void RL(string str)
{
  int len1 = str.IndexOf(':')+3;
int len = str.IndexOf('/', len1 , str.Length - len1 );
Console.WriteLine(str.Substring(0,len));
  Console.Read();
}



也就是说,把你要截取的url传给RL方法,就会返回给你域名

结果:http://bj.house.163.com

wait_happy 2010-07-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 q107770540 的回复:]
C# code

string str = "http://www.baidu.com/temp/abc.aspx?id=1";

str = str.Substring(str.LastIndexOf("/")+1, str.Length - str.LastIndexOf("/") - 2);
……
[/Quote]
..................
zhou499452352 2010-07-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 q107770540 的回复:]
C# code

string str = "http://www.baidu.com/temp/abc.aspx?id=1";

str = str.Substring(str.LastIndexOf("/")+1, str.Length - str.LastIndexOf("/") - 1);
……
[/Quote]
这个就可以啊!
symbol_bc 2010-07-09
  • 打赏
  • 举报
回复
正则比较好,可是我不会。

string s = str.Substring(str.LastIndexOf("/") + 1);
q107770540 2010-07-09
  • 打赏
  • 举报
回复

string str = "http://www.baidu.com/temp/abc.aspx?id=1";

str = str.Substring(str.LastIndexOf("/")+1, str.Length - str.LastIndexOf("/") - 1);
Response.Write(str);
//结果: abc.aspx?id=1
gxingmin 2010-07-09
  • 打赏
  • 举报
回复
string str = "http://www.baidu.com/temp/abc.aspx?id=1";

str = str.Substring(str.LastIndexOf("/")+1);
ivws_19 2010-07-09
  • 打赏
  • 举报
回复
string[] strArray=str.split('/');
if(strArray.Length>0)
{
//strArray[strArray.Length-1]即为所求
}
cpp2017 2010-07-09
  • 打赏
  • 举报
回复
来个更简单的

string str = "http://www.baidu.com/temp/abc.aspx?id=1";
Response.Write(System.IO.Path.GetFileName(str));
q107770540 2010-07-09
  • 打赏
  • 举报
回复

string str = "http://www.baidu.com/temp/abc.aspx?id=1";

str = str.Substring(str.LastIndexOf("/")+1, str.Length - str.LastIndexOf("/") - 2);
Response.Write(str);
FangYANYI 2010-07-09
  • 打赏
  • 举报
回复
string[] strArr=str.split('/');
string temp=strArr[strArr.Length-1].ToString();
zcxverygood123456 2010-07-09
  • 打赏
  • 举报
回复
有答案了
jin225 2010-07-09
  • 打赏
  • 举报
回复
这么多了 随便选个吧

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧