Parse out a charset from a content type header.
import java.util.regex.Matcher; import java.util.regex.Pattern; class Main { private static final Pattern charsetPattern = Pattern.compile("(?i)\\bcharset=\\s*\"?([^\\s;\"]*)"); /** * Parse out a charset from a content type header. * * @param contentType * e.g. "text/html; charset=EUC-JP" * @return "EUC-JP", or null if not found. Charset is trimmed and * uppercased. */ static String getCharsetFromContentType(String contentType) { if (contentType == null) return null; Matcher m = charsetPattern.matcher(contentType); if (m.find()) { return m.group(1).trim().toUpperCase(); } return null; } }
1. | A character queue with parsing helpers. |