【问题标题】:Trim url to last subfolder in VB将 url 修剪到 VB 中的最后一个子文件夹
【发布时间】:2015-01-23 07:18:17
【问题描述】:
【问题讨论】:
标签:
vb.net
string
url
directory
trim
【解决方案1】:
使用Substring 和Lastindex 的组合。像这样:
url.substring(0,url.lastindexof("/"))
可能是你需要从lastindexof("/")值中减去1,我总是忘记它^^
【解决方案2】:
使用 URL 时,请考虑使用 Uri 类。那么处理这种情况就变得容易了。
创建一个Uri 实例:
Dim url = new Uri("http://example.com/aa/bb/cc.html")
那么你可以这样做
Dim result = url.AbsoluteUri.Remove(url.AbsoluteUri.Length - url.Segments.Last().Length)
或类似的东西
Dim result = new Uri(url, ".").AbsoluteUri
【解决方案3】:
您可以使用 String.Remove() 删除字符串中不需要的部分:
Dim temp As String = "http://example.com/aa/bb/cc.html"
Dim index As String = temp.LastIndexOf("/"c)
Dim ret As String = temp.Remove(index, temp.Length - index)