2

If I receive an emailAddress in the following format:

example%40gmail.com

In Java how do I convert it to this:

example@gmail.com
Starus
  • 21
  • 1
  • 2

4 Answers4

6

Use URLDecoder.decode(String s, String enc) becuase URLDecoder.decode(String s) is deprecated in Java 1.5.

Here is the code to test your case:

@Test
public void testUrlDecoder() throws UnsupportedEncodingException {
    String encodedStr = "example%40gmail.com";
    String decodedStr = URLDecoder.decode(encodedStr, "UTF-8");
    assertEquals("example@gmail.com", decodedStr);
}
Alfredo Osorio
  • 10,997
  • 11
  • 52
  • 81
2

See the answer to this question: Java: How to unescape HTML character entities in Java?

Community
  • 1
  • 1
Naftali
  • 142,114
  • 39
  • 237
  • 299
2

This might be what you want, I haven't had a chance to test it to make sure that what you have is actually a url encoded item:

http://download.oracle.com/javase/1.4.2/docs/api/java/net/URLDecoder.html

RHSeeger
  • 15,654
  • 7
  • 50
  • 41
0

This might be a bit simplistic, but you could try:

email = myEmailAddress.getAddress();
email.replace("%40", "@");
myEmailAddress.setAddress(email);
MirroredFate
  • 11,736
  • 14
  • 65
  • 95
  • I don't know that this would work in all cases, though (probably not), so one of the other answers using decoders is better. – MirroredFate Jun 07 '11 at 16:32