作者:热血洒在青春中 | 来源:互联网 | 2023-02-05 10:13
I'm working on a project where my API returns url with id at the end of it and I want to extract it to be used in another function. Here's example url:
我正在开发一个项目,我的API在其结尾处返回带有id的url,我想将其解压缩以用于另一个函数。这是url的示例:
String advertiserUrl = http://../../.../uuid/advertisers/4 <<
At the moment I'm using java's string function called substring() but this not the best approach as IDs could become 3 digit numbers and I would only get part of it. Heres my current approach:
目前我正在使用java的字符串函数substring(),但这不是最好的方法,因为ID可能会成为3位数字,我只会得到它的一部分。继承了我目前的做法:
String id = advertiserUrl.substring(advertiserUrl.length()-1,advertiserUrl.length());
System.out.println(id) //4
It works in this case but if id would be e.g "123" I would only get it as "3" after using substring, so my question is: is there a way to cut/trim string using dashes "/"? lets say theres 5 / in my current url so the string would get cut off after it detects fifth dash? Also any other sensible approach would be helpful too. Thanks.
它适用于这种情况,但如果id为例如“123”,我只会在使用子字符串后得到它为“3”,所以我的问题是:有没有办法用破折号“/”剪切/修剪字符串?让我说当前网址中的theres 5 /所以字符串在检测到第五个破折号后会被切断?任何其他合理的方法也会有所帮助。谢谢。
P.s uuid in url may vary in length too
url中的P.suuid也可能在长度上有所不同
3 个解决方案