Starus
Starus

Reputation: 21

How to convert encoded email address in Java?

If I receive an emailAddress in the following format:

example%40gmail.com

In Java how do I convert it to this:

[email protected]

Upvotes: 2

Views: 4562

Answers (4)

Alfredo Osorio
Alfredo Osorio

Reputation: 11475

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("[email protected]", decodedStr);
}

Upvotes: 6

MirroredFate
MirroredFate

Reputation: 12825

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

email = myEmailAddress.getAddress();
email.replace("%40", "@");
myEmailAddress.setAddress(email);

Upvotes: 0

RHSeeger
RHSeeger

Reputation: 16282

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

Upvotes: 2

Naftali
Naftali

Reputation: 146310

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

Upvotes: 2

Related Questions