当前位置 : 主页 > 编程语言 > c++ >

表情编码与utf-8之间相互转换

来源:互联网 收集:自由互联 发布时间:2021-07-03
gistfile1.txt /** * 对表情进行转义成utf-8 * * @param str * @return */public static String emojiConvert1(String str) {String patternString = "([\\x{10000}-\\x{10ffff}\ud800-\udfff])";Pattern pattern = Pattern.compile(patternString);M
gistfile1.txt
/**
	 * 对表情进行转义成utf-8
	 * 
	 * @param str
	 * @return
	 */
	public static String emojiConvert1(String str) {
		String patternString = "([\\x{10000}-\\x{10ffff}\ud800-\udfff])";
		Pattern pattern = Pattern.compile(patternString);
		Matcher matcher = pattern.matcher(str);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			try {
				matcher.appendReplacement(sb, "[[" + URLEncoder.encode(matcher.group(1), "UTF-8") + "]]");
			} catch (Exception e) {
			}
		}
		matcher.appendTail(sb);
		return sb.toString();
	}

	/**
	 * 对数据进行表情转换
	 * 
	 * @param str
	 * @return
	 */
	public static String emojiRecovery2(String str) {
		String patternString = "\\[\\[(.*?)\\]\\]";
		Pattern pattern = Pattern.compile(patternString);
		Matcher matcher = pattern.matcher(str);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			try {
				matcher.appendReplacement(sb, URLDecoder.decode(matcher.group(1), "UTF-8"));
			} catch (UnsupportedEncodingException e) {
			}
		}
		matcher.appendTail(sb);
		return sb.toString();
	}
网友评论