Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best way to replace & in java string variable for URL variablepurpose.

Status
Not open for further replies.

KevinCO

Programmer
Oct 2, 2001
45
0
0
US
Hey, thanks for reading. I have a String that has an ampersand in it that I wish to use as a variable in a 'GET' URL. Is there a simple way to replace the ampersand with %26. The String.replace() seems only to work with single characters.
 
Hi,

Why dont you encode the string using URLEncoder. And you can decode the String once you received in next page.

jdk 1.3

String data = URLEncoder.encode("key1") + "=" + URLEncoder.encode("value1");
data += "&" + URLEncoder.encode("key2") + "=" + URLEncoder.encode("value2");

jdk 1.4

String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");

Similarly Decode the String using URLDecoder.

Cheers,
-Venu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top