[英]Remove everything before and after a string part in Android
我想從 URL 獲取亞馬遜產品 ASIN,這里有一些示例:
https://www.amazon.com/Premium-Flagship-Lenovo-Processor-Bluetooth/dp/B07H15QHT6/ref=sr_1_2_sspa?s=pc&ie=UTF8&qid=1537733386&sr=1-2-spons&keywords=laptop&psc=1
https://www.amazon.com/Youve-Got-Crabs-Creators-Exploding/dp/B07BKLT9B5/ref=mp_s_a_1_1?ie=UTF8&qid=1537733019&sr=8-1-spons&pi=AC_SX236_SY340_FMwebp_QL65&keywords=games&psc=1
我想在/dp/
之后獲取該文本並像這樣刪除該文本之后的所有內容
B07BKLT9B5 and B07H15QHT6
您可以使用:
split("dp")
並獲得 2 部分或使用substring()
和indexOf()
方法。
你可以做這樣的事情
public String getCode(String s) {
int pos = s.indexOf("/dp/"); // find the position of the string "/dp/" in the url
String substr = s.substring(pos + "/dp/".length()); // get everyting after dp
String[] parts = substr.split("/"); // split at every '/' character
return parts[0]; // your result will be the first element of the array
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.