删除所有角色我有一个这样的字符串:爪哇 - 获取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
不同。
我该如何删除其他字符?
删除所有角色我有一个这样的字符串:爪哇 - 获取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
不同。
我该如何删除其他字符?
下面的代码会从您的字符串中删除所有字母字符(使用正则表达式),并返回唯一数字字符:
String str = "Domain.com/agahi/view/330/some+text+here";
str = str.replaceAll("[^\\d]", "");
System.out.println(str);
结果:
330
谢谢,但链接是这样的:Domain.com/agahi/view/330/some+text+here+5 ? – graphist
在这种情况下(如果只需要字符串的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
'字符串ID = yourString.replaceAll( “Domain.com/agahi/view/(\\d+).*”,“$ 1”)' –