Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding URL strings with regular expression

Tags:

java

regex

url

I'm trying to replace several different characters with different values. For example, if I have: #love hate then I would like to do is get back %23love%20hate

Is it something to do with groups? i tried to understand using groups but i really didn't understand it.

like image 847
BeginnerPro Avatar asked Jan 27 '26 02:01

BeginnerPro


1 Answers

You can try to do this:

String encodedstring = URLEncoder.encode("#love hate","UTF-8");

It will give you the result you want. To revers it you should do this:

String loveHate = URLDecoder.decode(encodedstring);
like image 111
MatBanik Avatar answered Jan 28 '26 14:01

MatBanik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!