0

I need to substract an specific parameter (urlReturn) from a URL like this :

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

My final string should look like this:

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

And the rest of the string should look like this:

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

I currently have this :

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

But the urlReturn parameter should always come as the first one.

2
  • 2
    And what have you done? Commented Jan 10, 2014 at 23:34
  • Show us what you have done. It'd take a one substring and a couple of indexOf to get what you want. Commented Jan 10, 2014 at 23:42

2 Answers 2

1

Try this (s is your original String):

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

Definitely not elegant at all, but working. I really need some sleep now so take my anser carefully :) You may alswo wanto to check if the parameters is in the String s via the contains method to avoid index out of bounds exceptions.

Sign up to request clarification or add additional context in comments.

Comments

0

use string#split

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

4 Comments

This doesn't produce anything that's close to what OP is asking for.
@Keppil it lets him extract a part that he needs. He'd of course need to split again to get the first part, but I'm not going to do all the work for him. And I wouldn't bother with regex because if he's asking this it's probably beyond his skillset for the time being.
No, it lets him extract a part of what he needs, while discarding parts of what he wants to keep.
@Keppil you can still do another split on the same original string and then concatenate things together

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.