2017-02-18 46 views
0

刪除所有角色我有一個這樣的字符串:爪哇 - 獲取ID從鏈接

Domain.com/agahi/view/330/some+text+here 

OR

Domain.com/agahi/view/330/ 

OR

Domain.com/agahi/view/330 

什麼辦法,我必須從獲取ID此鏈接(本示例中爲330

注意:也許some+text+here不同。

我該如何刪除其他字符?

+0

'字符串ID = yourString.replaceAll( 「Domain.com/agahi/view/(\\d+).*」,「$ 1」)' –

回答

1

下面的代碼會從您的字符串中刪除所有字母字符(使用正則表達式),並返回唯一數字字符:

String str = "Domain.com/agahi/view/330/some+text+here"; 
str = str.replaceAll("[^\\d]", ""); 

System.out.println(str); 

結果:

330 
+0

謝謝,但鏈接是這樣的:Domain.com/agahi/view/330/some+text+here+5 ? – graphist

+0

在這種情況下(如果只需要字符串的330部分),可以使用「/」作爲分隔符來分割它: String str =「Domain.com/agahi/view/330/some+text+here +5" ; String [] id = str.split(「/」); String result = id [3]; 如果你仍然想去掉字母字符的id,你可以繼續使用: System.out.println(result.replaceAll(「[^ \\ d]」,「」)); – inokien