我的网址链接在下方指定 -
如何解析上述网址,以便在“个人资料”实体后获取值“756435”。
java新手
感谢任何帮助
答案 0 :(得分:1)
我可能会使用这样的东西
String in = "http://newyork.citycompass.com/profile/756435/newy"; // cut...
String toMatch = "profile";
int pos = in.indexOf(toMatch);
String profileId = "";
if (pos > -1) {
pos += toMatch.length();
int pos2 = in.indexOf('/', pos + 2);
if (pos2 > -1) {
profileId = in.substring(pos + 1, pos2);
}
}
System.out.println(profileId);
答案 1 :(得分:0)
您可以使用matcher
查找http://newyork.citycompass.com/profile/这样的内容:
Pattern pattern = Pattern.compile("http://newyork.citycompass.com/profile/");
Matcher matcher = pattern.matcher(your_url_string_here);
if (matcher.find()){
// If it is always 6 numbers
String number = your_url_string_here.substring(matcher.end(),matcher.end()+6);
//If it isn't always six numbers you could use indexOf to find the next / and then select between matcher.end() and the next slash.
}
答案 2 :(得分:0)
url.split("/")[4]
输出:
756435