flybywire
flybywire

Reputation: 273802

Remove key=value from url

I need a java library that will make it easy to edit URLs. URLs are given as Strings and must be returned as strings.

I need things such as removing key=value pairs and leaving the url clean and with ?s and &s properly placed.

Upvotes: 0

Views: 346

Answers (3)

RudolphEst
RudolphEst

Reputation: 1250

Use regular expressions and pattern matching:

For instance:

String original = "http://www.someHost.com/somePage?key1=value1&key2=value2";
Pattern keyValPattern = Pattern.compile("\\p{Alpha}\\w+=[^&]+");
Matcher m = keyValPattern.matcher(original);  
m.find(); // find an occurence of key=value pair
String keyVal = m.group(); // get the value of the found pair
// keyVal will be 'key1=value1'
int start = m.start(); // the start index of 'key1=value1' in the original string
int end = m.end(); //the end index of 'key1=value1' in the original string
m.find();
String keyVal2 = m.group();// keyVal2 will be 'key2=value2'
// ... etc

Upvotes: 2

partlov
partlov

Reputation: 14277

This is simple utility class which can help. I wouldn't use whole library just for that, better is to see what you needs and create your own utility class with methods which you need.

Upvotes: 0

user1596371
user1596371

Reputation:

Use the Java URL class to parse the original string and change the parameters, then just toString() it again for your result.

Upvotes: 0

Related Questions