过滤3个字节以上的utf-8字符
/**
* 过滤掉超过3个字节的UTF8字符
* @param text
* @return
* @throws UnsupportedEncodingException
*/
public static String filterOffUtf8Mb4(String text) throws UnsupportedEncodingException {
byte[] bytes = text.getBytes("utf-8");
ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
int i = 0;
while (i < bytes.length) {
short b = bytes[i];
if (b > 0) {
buffer.put(bytes[i++]);
continue;
} b += 256; // 去掉符号位 if (((b >> 5) ^ 0x6) == 0) {
buffer.put(bytes, i, 2);
i += 2;
} else if (((b >> 4) ^ 0xE) == 0) {
buffer.put(bytes, i, 3);
i += 3;
} else if (((b >> 3) ^ 0x1E) == 0) {
i += 4;
} else if (((b >> 2) ^ 0x3E) == 0) {
i += 5;
} else if (((b >> 1) ^ 0x7E) == 0) {
i += 6;
} else {
buffer.put(bytes[i++]);
}
}
buffer.flip();
return new String(buffer.array(), "utf-8");
}
过滤3个字节以上的utf-8字符的更多相关文章
- 《Python CookBook2》 第一章 文本 - 过滤字符串中不属于指定集合的字符 && 检查一个字符串是文本还是二进制
过滤字符串中不属于指定集合的字符 任务: 给定一个需要保留的字符串的集合,构建一个过滤函数,并可将其应用于任何字符串s,函数返回一个s的拷贝,该拷贝只包含指定字符集合中的元素. 解决方案: impor ...
- dedecms功能性函数封装(XSS过滤、编码、浏览器XSS hack、字符操作函数)
dedecms虽然有诸多漏洞,但不可否认确实是一个很不错的内容管理系统(cms),其他也不乏很多功能实用性的函数,以下就部分列举,持续更新,不作过多说明.使用时需部分修改,你懂的 1.XSS过滤. f ...
- encode_utf8 把字符编码成字节 decode_utf8解码UTF-8到字符
encode_utf8 $octets = encode_utf8($string); Equivalent to "$octets = encode("utf8", $ ...
- 位(bit)、字节(byte)、字符、编码之间的关系
1.位: 数据存储的最小单位.每个二进制数字0或者1就是1个位: 2.字节: 8个位构成一个字节:即:1 byte (字节)= 8 bit(位): 1 KB = 1024 B(字节): 1 MB = ...
- 基础:位(bit)、字节(byte)、字符、编码之间的关系
1.位: 数据存储的最小单位.每个二进制数字0或者1就是1个位: 2.字节: 8个位构成一个字节:即:1 byte (字节)= 8 bit(位): 1 KB = 1024 B(字节): 1 MB = ...
- C语言实现过滤ASCII在0~127范围内的字符,并去除重复的字符
#include <stdio.h> #include <string.h> /* 1.以字符串作为参数 2.找出ASCII在1~127范围内的字符 3.去掉重复字符 */ i ...
- AndroidのInputFillter之按字符过滤长度,一个中文当两个字符
/** * 以Byte数的方式来实现的LengthFilter * @author bvin */ public class OneByteInputFilter implements InputFi ...
- 【异常记录(六)】vs文件乱码:文件加载,使用Unicode(UTF-8)编码加载文件xxx时,有些字节已用Unicode替换字符替换。保存该文件将不会保留原始文件内容。
VS2013偶遇这种情况,页面汉字编码出现乱码. .... 按照网上查到的: 工具>选项>文本编辑器> 勾选了 然并卵,还是乱码... 其实炒鸡简单 用记事本打开另存为,选择 ...
- 刨根究底字符编码之十一——UTF-8编码方式与字节序标记
UTF-8编码方式与字节序标记 一.UTF-8编码方式 1. 接下来将分别介绍Unicode字符集的三种编码方式:UTF-8.UTF-16.UTF-32.这里先介绍应用最为广泛的UTF-8. 为满足基 ...
随机推荐
- Thinking Clearly about Performance
http://queue.acm.org/detail.cfm?id=1854041 The July/August issue of acmqueue is out now acmqueue is ...
- Resource Manager
Azure Resource Manager overview https://azure.microsoft.com/en-us/documentation/articles/resource-gr ...
- set and Sequence theory
https://en.wikipedia.org/wiki/Class_(set_theory) https://en.wikipedia.org/wiki/Zermelo%E2%80%93Fraen ...
- vb.net 动态调用api
Imports System Imports System.Runtime.InteropServices Public Class DllInvoke Public Sub New(ByVal DL ...
- ABBYY FineReader 12最新官方版下载
ABBYY FineReader是市场领先的文字识别(OCR)软件,可快速方便地将扫描纸质文档.PDF文件和数码相机的图像转换成可编辑.可搜索的信息,ABBYY FineReader 12是目前最新版 ...
- 005. C#发送邮件
/// <summary> /// 发送邮件 /// </summary> /// <param name="toEmailS">邮件接收者列表 ...
- web.xml文件中配置<mime-mapping>下载文件类型
TOMCAT在默认情况下下载.rar的文件是把文件当作text打开,以至于IE打开RAR文件为乱码,如果遇到这种情况时不必认为是浏览器的问题,大多数浏览器应该不会死皮赖脸地把二进制文件当作文本打开,一 ...
- .NET去掉HTML标记
using System.Text.RegularExpressions; /// <summary> /// 去除HTML标记 /// </summary> /// < ...
- IOS开发-UIBarButtonItem系统自带图标总结
1.这四个返回的是后面的单词. UIBarButtonSystemItemDone UIBarButtonSystemItemCancel UIBarButtonSystemItemEdit UIBa ...
- sql 操作,
SELECT * FROM dbo.tbl_Web_Photos AS pt LEFT JOIN dbo.tbl_Web_Friends AS fd ON pt.user_email=fd.AddEm ...