0

我有一對夫婦網址:java中的字符串URL尋找一個子URL

a. https://iamterribleatthis/a 

b. https://iamterribleatthis/a/index.html 

我使用Apache stringutls,看是否「一」出現在「B」,但我認爲,正斜槓沒有通過比較。是否有更容易/更好的方法來查找'b'中是否存在'a',包括正斜槓?謝謝。

+4

顯示你的代碼可能會有所幫助的一部分。理想情況下,斜槓不應在這裏造成任何問題。但爲什麼你需要'Apache StringUtils'類,而不是僅僅使用'String#contains()'方法? – 2015-02-24 19:11:48

+0

如果'b'包含'a',你想查找嗎?或最長的匹配子字符串? – erencan 2015-02-24 19:14:28

+1

@RohitJain謝謝。它看起來像'/'導致了一個問題,但是當我檢查我的代碼時,我發現了一個導致問題的錯字。 – user6123723 2015-02-24 19:17:37

回答

1
String a = "https://iamterribleatthis/a"; 
    String b = "https://iamterribleatthis/a/index.html"; 

    System.out.println(b.contains(a)); 
1

轉換的URL,他的外形和使用indexOf()

b.toExternalForm().indexOf(a.toExternalForm()) 

如果返回值是大於-1是說明B