【问题标题】:How to find an index position of first occurrence of forward slash in URL?如何在 URL 中找到第一次出现正斜杠的索引位置?
【发布时间】:2018-05-05 10:56:46
【问题描述】:

如何从这个字符串中找到第一次出现/正斜杠的索引位置?

String str = "http://somewhere.com/abc";

我尝试过使用str.substring(str.indexOf('/')),但它返回//somewhere.com/abc。我想要的只是提取/abc。

【问题讨论】:

  • 好吧,你想要 last 斜杠的索引,而不是第一个。第一个斜线是紧跟在http: 之后的斜线。 javadoc 是你的朋友。
  • 如果可以出现任意数量的斜线,例如http://somewhere.com/abc/xyz/def 和协议已修复,然后您可以使用 indexOf 的变体作为 str.indexOf('/', str.indexOf('://') + 3)(从索引 7 开始查找“/”)
  • 阅读javadoc。或者只是这样做。 str.substring(str.IndexOf(".com/")+4)

标签: java substring


【解决方案1】:

这就是我所做的: String str = "http://somewhere.com/abc";字符串 [] arr= str.split("/"); String string = arr[arr.length-1];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多