Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java将表情符号从字符串替换为字符串_Java_Emoticons - Fatal编程技术网

使用java将表情符号从字符串替换为字符串

使用java将表情符号从字符串替换为字符串,java,emoticons,Java,Emoticons,如何从字符串中删除表情符号我的简单代码是 public static void main(String[] args) throws SQLException { String str="My nam is ur -D "; getRefineCode(str); } private static void getRefineCode(String str) throws { List smstypeWord=getshortWord(); for(int i

如何从字符串中删除表情符号我的简单代码是

public static void main(String[] args) throws SQLException {
    String str="My nam is  ur -D ";
    getRefineCode(str);
}

private static void getRefineCode(String str) throws {
    List smstypeWord=getshortWord();
    for(int i=0;i<smstypeWord.size();i++) {
        String string=smstypeWord.get(i).toString();
        String stringcon[]=string.split("_");
        String emessage=stringcon[0];
        String emoticon=stringcon[1].trim();
        if(str.contains(emoticon)) {
            str=str.replace(emoticon, emessage);
            System.out.println("=================>"+str);
        }   
    }
    System.out.println("=======++==========>"+str);
}

private static List getshortWord() throws SQLException {
    String query1 = "SELECT * FROM englishSmsText";
    PreparedStatement ps = conn.prepareStatement(query1);
    ResultSet rs = ps.executeQuery();
    String f_message="";
    String s_message="";
    while(rs.next()) {
        s_message=rs.getString("message");
        f_message=rs.getString("short_text");
        shortMessage.add(s_message+"_"+f_message);
        //fullMessage.add(f_message);
    }
    return shortMessage;
}
publicstaticvoidmain(字符串[]args)抛出SQLException{
String str=“我的名字是ur-D”;
编码(str);
}
私有静态void getRefineCode(字符串str)抛出{
列表smstypeWord=getshortWord();

对于(int i=0;i首先,
replace
应该是
replaceAll
,否则您只会看到第一次出现的表情符号或缩写

第二,您可以通过只匹配整个单词来减少误报的数量。
replaceAll
接受正则表达式,因此您可以使用
replaceAll(“\\b”+emoticon+“\\b”,emessage)
只替换被单词边界(空格、标点符号等)包围的缩写

然而,在你使用的词典中,你仍然会将
KISS
替换为
保持简单、愚蠢
。你会将
86
替换为
“退出”或“结束”或“摆脱”
。也许你应该寻找一种不同的方法

编辑:我忘了您正在寻找特殊字符。您应该尝试类似于此正则表达式的操作,它将抑制搜索字符串中的特殊字符(并且将比以前过于严格的
\b
模式更加慷慨):


replaceAll((?您可能应该使用
replaceAll
而不是
replace
。除此之外,我看不到任何明显的变化,观察到的与期望的行为是什么?您可以发布一个吗?代码段中是否有人试图解决您自己的问题,或者代码段是否等于“请完成”“?但当字符串包含'B-'正则表达式时error@SoniaGupta请参阅我的编辑。((?
)的意思是什么?
replaceAll("((?<=\\W)|^)\\Q" + emoticon + "\\E((?=\\W)|$)", emessage);