2014-01-10 43 views
0

我需要从一个URL。减去一个特定的参数(urlReturn)是这样的:。减去特定参数

http://somedomain.mx/th=6048000&campus=A&matric=L01425785&serv=GRAD&count=2&per=TEST&passwd=69786e720c0f0e&mount=1000.00&urlReturn=http://somedomain.mx:7003/SP/app/login.xhtml?id=1234&mat=2323&fh=05012014124755&store=TESO

我最终的字符串应该是这样的:

String urlReturn = http://somedomain.mx:7003/SP/app/login.xhtml?id=1234&mat=2323;

而其余的字符串应该看起来像这样:

String urlReturn2 = http://somedomain.mx/th=6048000&campus=A&matric=L01425785&serv=GRAD&count=2&per=TEST&passwd=69786e720c0f0e&mount=1000.00&fh=05012014124755&store=TESO

目前,我有这样的:

String string = string.toString().split("\\&")[0]; 

但urlReturn参数应该总是作为第一个。

+2

你做了什么? –

+0

告诉我们你做了什么。它需要一个子字符串和一对indexof来获得你想要的。 – elbuild

回答

0

使用string#split

url.split("\\?")[1]; 
+0

这不会产生任何接近OP所要求的东西。 – Keppil

+0

@Keppil它可以让他提取他需要的部分。他当然需要再次分手以获得第一部分,但我不会为他完成所有的工作。我不会因为正则表达式而烦恼,因为如果他问这个问题,可能暂时超出他的技能。 –

+0

不,它让他提取他所需要的一部分,同时丢弃他想要保留的部分。 – Keppil

1

试试这个(s是你原来的字符串):

urlReturn = s.substring(0, s.indexOf("&urlReturn=")).replace("&urlReturn=", ""); 
urlReturn2 = s.substring(s.indexOf("&urlReturn=")).replace("&urlReturn=", ""); 

非也优雅可言,但工作。我现在真的需要一些睡眠,所以仔细看看我的anser :)你也可能想通过contains方法来检查参数是否在String中,以避免索引超出范围异常。