【问题标题】:Trim url to last subfolder in VB将 url 修剪到 VB 中的最后一个子文件夹
【发布时间】:2015-01-23 07:18:17
【问题描述】:

我在 VB 中有一个字符串: url = "http://example.com/aa/bb/cc.html"

我想将此网址修剪到最后一个子文件夹,使其变为: url = "http://example.com/aa/bb"

我需要删除最后一个“/”之后的所有内容。 我正在考虑使用 string.lastindexof("/") 方法,但不知道如何从那里继续。

【问题讨论】:

    标签: 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)
        

        【讨论】:

          猜你喜欢
          • 2021-01-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-05
          • 1970-01-01
          • 2010-11-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多