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
  • 11,297
  • 12
  • 56
  • 84
2

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

Community
  • 1
  • 1
Naftali
  • 144,921
  • 39
  • 244
  • 303
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
  • 16,034
  • 7
  • 51
  • 41
0

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

email = myEmailAddress.getAddress();
email.replace("%40", "@");
myEmailAddress.setAddress(email);
MirroredFate
  • 12,396
  • 14
  • 68
  • 100
  • 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