简体   繁体   中英

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:

example@gmail.com

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);
}

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM