Java String Decode decodeText(String s)

Here you can find the source of decodeText(String s)

Description

Decode iso-xxxx strings present in subjects and emails like: =?ISO-8859-1?Q?No=20hay=20ma=F1ana?=

License

Apache License

Declaration

public static String decodeText(String s) 

Method Source Code

//package com.java2s;
/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you under the Apache License, Version 2.0 (the            *
 * "License"); you may not use this file except in compliance   *
 * with the License.  You may obtain a copy of the License at   *
 *                                                              *
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 *                                                              *
 * Unless required by applicable law or agreed to in writing,   *
 * software distributed under the License is distributed on an  *
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
 * KIND, either express or implied.  See the License for the    *
 * specific language governing permissions and limitations      *
 * under the License.                                           *
 ****************************************************************/

import java.io.UnsupportedEncodingException;

import javax.mail.internet.MimeUtility;

public class Main {
    /**// w ww. jav  a 2s . c  om
     * Decode iso-xxxx strings present in subjects and emails like:
     *
     * =?ISO-8859-1?Q?No=20hay=20ma=F1ana?= <hello@hupa.org>
     */
    public static String decodeText(String s) {
        String ret = s;
        try {
            ret = MimeUtility.decodeText(s);
        } catch (UnsupportedEncodingException e) {
            System.out.println(e.getMessage());
        }
        ret = ret
                // Remove quotes around names in email addresses
                .replaceFirst("^[<\"' ]+([^\"<>]*)[>\"' ]+<", "$1 <");
        return ret;
    }
}

Related

  1. decodeString(String str)
  2. decodeString(StringReader in)
  3. decodeStringFromByteArray(byte[] data)
  4. decodeStrings(byte[] stringBytes)
  5. decodeText(String encodeText)
  6. decodeText(String str)
  7. decodeTextQuietly(String input)