string判断或者过滤字符串特殊字符

下面展示一些 内联代码片

判断是否包含特殊字符

// An highlighted block

	public static boolean containsEmoji(String source) {
		int len = source.length();
		boolean isEmoji = false;
		for (int i = 0; i < len; i++) {
			char hs = source.charAt(i);
			if (0xd800 <= hs && hs <= 0xdbff) {
				if (source.length() > 1) {
					char ls = source.charAt(i + 1);
					int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
					if (0x1d000 <= uc && uc <= 0x1f77f) {
						return true;
					}
				}
			} else {
				// non surrogate
				if (0x2100 <= hs && hs <= 0x27ff && hs != 0x263b) {
					return true;
				} else if (0x2B05 <= hs && hs <= 0x2b07) {
					return true;
				} else if (0x2934 <= hs && hs <= 0x2935) {
					return true;
				} else if (0x3297 <= hs && hs <= 0x3299) {
					return true;
				} else if (hs == 0xa9 || hs == 0xae || hs == 0x303d
						|| hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c
						|| hs == 0x2b1b || hs == 0x2b50 || hs == 0x231a) {
					return true;
				}
				if (!isEmoji && source.length() > 1 && i < source.length() - 1) {
					char ls = source.charAt(i + 1);
					if (ls == 0x20e3) {
						return true;
					}
				}
			}
		}
		return isEmoji;
	}

过滤特殊字符

// An highlighted block
private static boolean isEmojiCharacter(int codePoint) {
		return (codePoint >= 0x2600 && codePoint <= 0x27BF) // 杂项符号与符号字体
				|| codePoint == 0x303D
				|| codePoint == 0x2049
				|| codePoint == 0x203C
				|| (codePoint >= 0x2000 && codePoint <= 0x200F)//
				|| (codePoint >= 0x2028 && codePoint <= 0x202F)//
				|| codePoint == 0x205F //
				|| (codePoint >= 0x2065 && codePoint <= 0x206F)//
				/* 标点符号占用区域 */
				|| (codePoint >= 0x2100 && codePoint <= 0x214F)// 字母符号
				|| (codePoint >= 0x2300 && codePoint <= 0x23FF)// 各种技术符号
				|| (codePoint >= 0x2B00 && codePoint <= 0x2BFF)// 箭头A
				|| (codePoint >= 0x2900 && codePoint <= 0x297F)// 箭头B
				|| (codePoint >= 0x3200 && codePoint <= 0x32FF)// 中文符号
				|| (codePoint >= 0xD800 && codePoint <= 0xDFFF)// 高低位替代符保留区域
				|| (codePoint >= 0xE000 && codePoint <= 0xF8FF)// 私有保留区域
				|| (codePoint >= 0xFE00 && codePoint <= 0xFE0F)// 变异选择器
				|| codePoint >= 0x10000; // Plane在第二平面以上的,char都不可以存,全部都转
	}

public static String filterEmoji(String src) {
		if (src == null) {
			return null;
		}
		int cpCount = src.codePointCount(0, src.length());
		int firCodeIndex = src.offsetByCodePoints(0, 0);
		int lstCodeIndex = src.offsetByCodePoints(0, cpCount - 1);
		StringBuilder sb = new StringBuilder(src.length());
		for (int index = firCodeIndex; index <= lstCodeIndex;) {
			int codepoint = src.codePointAt(index);
			if (!isEmojiCharacter(codepoint)) {
				System.err.println("codepoint:" + Integer.toHexString(codepoint));
				sb.append((char) codepoint);
			}
			index += ((Character.isSupplementaryCodePoint(codepoint)) ? 2 : 1);

		}
		return sb.toString();
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值