链接字符串的子串

Hay*_*aed 3 java string

我想只获得以下链接字符串的实际链接:

String link = <a href="http://www.facebook.com/wwwausedu" target="_blank" class="btnFacebook">Link to Facebook</a>
Run Code Online (Sandbox Code Playgroud)

结果应该只是 www.facebook.com/wwwausedu

我尝试了以下但它不起作用:

TEMP = link.substring(link.indexOf("http://")+1, tmp.lastIndexOf("\""));
Run Code Online (Sandbox Code Playgroud)

mgi*_*nbr 5

你并不需要的最后一个索引",但第一个http://:

TEMP = link.substring(link.indexOf("http://")+7, link.indexOf("\"", link.indexOf("http://")));
Run Code Online (Sandbox Code Playgroud)

String.indexOf(String str, int fromIndex)函数str在指定的索引之后首次出现.另外,正如@mellamokb the Wise所指出的那样,您需要添加7到索引中,而不是1,因为您http://要从结果中排除.

  • +1你还需要`link.indexOf("http://")+ 7`,而不是`+ 1`(http://ideone.com/QPNnlR) (2认同)
  • @HayaRaed:`indexOf`在开头匹配.所以当它找到`http://`时,它会返回它找到的表达式的*start*处的索引,就在`h`之前.既然你想要`TEMP`以`www`开头,你需要偏移`http://`的长度,这是7个字符长. (2认同)