2015-06-11 154 views
5

所以我試圖提取URL的/分隔符之間的字符串。由於Java不接受「/」作爲字符,這對我來說已經證明有點困難。如何提取兩個「/」字符之間的字符串

String temp = "svn+ssh://xxxxxx.net/var/lib/webprojects/xxx/xxx/WebContent/images/Calendar_icon.png"; 

我怎麼能從這String : images , WebContent , etc...

+3

「* Java不接受‘/’作爲一個char *」是什麼那是什麼意思? – Biffen

+0

@Jordi注意''/「'是一個字符串,'/ /'是一個字符。 – Alderath

+0

@Jordi Yo你可能想使用'java.net.URI'和它的'getPath()'來首先獲取路徑。 – Biffen

回答

7

如何通過/分割字符串 -

String[] parts = temp.split("/"); 

,並通過它們的索引位置訪問相關部分。

2

嘗試用繩子

String[] stringArray = temp.split("/"); 
3

首先拆分功能,使用正則表達式來刪除不想要的東西:

String temp = temp.replaceAll("^.*//xxxxxx.net",""); 

則將其分解:

String[] parts = temp.split("/"); 
3

你應該使用split函數。有了這個,你將把你的String劃分爲一個字符串數組,它放置在「/」的位置。因此,舉例來說,如果你要訪問的網址,你的情況將是:

String split[] = temp.split("/"); //Here you have your String divide. 

因此,要訪問URL(看它是在陣列的第二位置(因爲在第一個位置是svn+ssh:),你將不得不這樣做!?

split[1]; 

我希望這將是對你有所幫助

相關問題