如何在网址中获取部分内容?

时间:2015-03-10 09:14:29

标签: java string

URL 1:http://cr2.shopping.naver.com/adcr.nhn X = dhjnSwL99ITWnTtJ%2BNcbif%2F%2F%2Fw%3D%3Ds5rXuTvXvEeaqd1LMapU%2FU%2FGYs5Bq2xhAtWlmO9X%2ByC0HNdgYZa86Dt%2FS%2Fz7OEUUL1hAHF8%2BM4HFSjImNC801nZTDskEtEQbmTqBaIjRJ2Zrgb%2BRdlzPDKW4FFk5yTMTbuTDME%2BhnaIrjajOgxCIrF6Ab7qWwAUAaFXmwYSYm2%2BobW8Q0n1K624WPXQnYRAFxDPAb7dS%2BezuSpS7lne63UwXLaZMsvFkF2BbMDWNeydNCL47yrheZprULMzcvv9y45R3tTbxpYoaYHpLGc %2Bgonz70eUtzBzAvuV%2FSEH06pBp86oeMlgiki9dbDwP9PZmKXmLuhaukQT%2B9%2FMhnlSnrMWGO6LT9sQoYgy%2F7nDF6k8t5KfQvg9rDMmSRUEAalmGCqaBTMNq3zchwlX98MJHPdV6F1eprzVOqyZHx2uCj%2FKjclwziyFVZHP1hD3WCYVjtuY9klE%2BUwceaYdBNdemgQlecrAFITNNiFB8Tm%2FsTSWLuh14vREq%2FGGjSmMm%2B0uw%2B%2BpIkCoKahUdLQHbfDcWRs9adZpUKxMQ%2BelQL1PiMqWExew7ToVYx78MKeYrX0Rxx9skhAYhPiPVASQ40oPriMg%3D%3D &安培; nv_mid = 8092039238&安培; CAT_ID = 50002054

URL 2:http://cr2.shopping.naver.com/adcr.nhn X = T1M9wt6JEhq9K7FSJCG%2B4P%2F%2F%2Fw%3D%3Ds42a0gCIQ3fSoQ%2FEVEIVy%2BPGYs5Bq2xhAtWlmO9X%2ByC0IwY84pBogsHz4%2FkZhaYX09wXTpW0ysnyNtuaQs0AnyzXcJa7O5c%2FAbqG%2B46W8cYx5BLLrGiz9Lcdu3uNlSFkaLb8mb5Z3SwCWQIP4ZlXmTy8lVyOVkZbUxHBxtO4FYsK4xPqTAqq7c73Ub%2BAx%2BWuVhebCC3WbDgb6s13t6dhbz6%2Fm3qLgfYP8mpGJNNAmOsEsy0AMLM4z0sMQ3TaEwL5Pa3kNmrsGZGMwIvYg424wN8fzI54Dvwc1MhnN8cYkoYUdbNcIIEWSfPOPj6XPleUZoV78mxVOVsjND%2BCWEhgf5ZhxgkZwwg %2F7PYaL0vrdYCbD9nqbxtnpJ64t3tx6gK%2FGHsFgp6kcG0ZzT8IjCzTPTrXk%2FppF3RDrYdLCGP%2BBjRIfHyaFlZ621AZVGsQCqBxCyLZeFzVun%2BDlZ3K8pIZ4Q49WO4Pow4N4AOr0R2ic5VwopTaiAicvPcJTFXkUQW8SKEArO7HQDTRJyR893w7KyaVL1evn5V%2FVx08i9phKSsfyN20pO6Rhh7d6%2BViGP2vJjgvLF6hYMCZ12QNYa%2Biz4pPLPgEpopImoxepm0ZGzyvGozpTQRWy9WvvHVoeOWG5X1Rl4FzrhSwsFfvy2dKqRA%3D%3D &安培; nv_mid = 8092039238&安培; CAT_ID = 50002054

你好朋友。 我正在学习Java。我想获得一些URL的部分内容。请看上面的网址,我想得到" x" url中的属性(粗体)如何得到" x = ......" ?

我使用了这段代码:

 String link = text.substring(39, 734); 

但如果url的长度不同,则代码不正确。 如何在上面的网址上获得确切的价值? 我希望在&strong; 之前从 x = 转到

2 个答案:

答案 0 :(得分:1)

使用URI类:

final URI uri = new URI(yourUrlHere);
final String query = uri.getQuery();

然后,您可以从query

中提取所需的文字

此外,您可以执行大量其他检查,因为URI类有很多方法可以获取不同的组件等。有关详细信息,请参阅the javadoc

答案 1 :(得分:0)

String url = url1;
int start = url.indexOf("x=");
int end = url.indexOf("&nv_mid=");
String value = url.substring(start, end);