Java implementation filters out emoticons in text

Source: Internet
Author: User

For example, we want to put the nickname into the database, we need to filter out the expression.

Package Com.zjr.common.utils;import Org.apache.commons.lang3.stringutils;public class Emojifilter {/** * detects if there are emoji characters * * @param source * @return throws */public static Boolean Containsemoji (String source) {if (Stringutils.isblank (source)) once it contains ) {return false;} int len = Source.length (), for (int i = 0; i < len; i++) {Char codepoint = Source.charat (i); if (Isemojicharacter (codepoi NT)) {//Do nothing, judging here indicates that there is an emoticon character return true;}} return false;} private static Boolean isemojicharacter (char codepoint) {return (codepoint = = 0x0) | | (codepoint = = 0x9) | | (codepoint = = 0xA) | | (codepoint = = 0xD) | | ((codepoint >= 0x20) && (codepoint <= 0xd7ff)) | | ((codepoint >= 0xe000) && (codepoint <= 0xFFFD)) | | ((codepoint >= 0x10000) && (codepoint <= 0x10ffff));} /** * Filter emoji or other non-literal type characters * * @param source * @return */public static string Filteremoji (string source) {if (!containse Moji (source)) {return source;//if not included, return directly to}//here is definitely included StringBuilder buf = Null;int len = Source.length (); for (int i = 0; i < len; i++) {Char codepoint = Source.charat (i); if (Isemojicharacter (codepoint)) {if (BUF = = null) {buf = new StringBuilder (Source.length ());} Buf.append (codepoint);} }if (buf = = null) {return source;//If no emoji expression is found, return the source string} else {if (buf.length () = = Len) {//The meaning here is as little as ToString, because it will be re-born into string buf = Null;return source;} else {return buf.tostring ();}}}}


Java implementation filters out emoticons in text

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.