给定字符串中的混合重音和普通字符在搜索时在java中不起作用 [英] Given mixed accented and normal characters in string not working in java when searching

查看:139
本文介绍了给定字符串中的混合重音和普通字符在搜索时在java中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String text = "Cámélan discovered ônte red aleŕt \n Como se extingue la deuda";

如果我给出输入Ca,它应该从给定的字符串Cá突出显示,但它不突出显示。

If I give the input Ca, it should highlight from the given string Cá but it's not highlighting.

以下是我的尝试。

 Pattern mPattern; 
  String filterTerm; //this is the input which I give from input filter. Say for eg: Ca
   String regex = createFilterRegex(filterTerm);
        mPattern = Pattern.compile(regex);

 private String createFilterRegex(String filterTerm) {
        filterTerm = Normalizer.normalize(filterTerm, Normalizer.Form.NFD);
       filterTerm = filterTerm.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
        return filterTerm;
    }

public Pattern getPattern() {
        return mPattern;
    }

在另一个班级,

private SpannableStringBuilder createHighlightedString(String nodeText, int highlightColor) { //nodeText is the entire list displaying. 
        SpannableStringBuilder returnValue = new SpannableStringBuilder(nodeText);
        String lowercaseNodeText = nodeText;
        Matcher matcher = mFilter.getPattern().matcher((createFilterRegex(lowercaseNodeText)));
        while (matcher.find()) {
            returnValue.setSpan(new ForegroundColorSpan(highlightColor), matcher.start(0),
                    matcher.end(0), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        }

        return returnValue;
    }

viewHolder.mTextView.setText(createHighlightedString((node.getText()), mHighlightColor));

viewHolder.mTextView.setText(createHighlightedString((node.getText()), mHighlightColor));

但我得到的输出是,

如果我单独输入单个字母o ,它突出显示,但如果我传递两个以上的字母表,例如:Ca,它不会突出显示。我无法弄清楚我在做什么错。

If I type single alphabet o alone, it's highlighting but if I pass more than two alphabets say for eg: Ca, it's not highlighting and displaying. I couldn't figure out what mistake I am doing.

但如果你看看WhatsApp。它已经实现了。

But if you look WhatsApp. it has been achieved.

我键入了Co,它识别并突出显示句子中的重音字符。

I typed Co, it's recognizing and highlighting accented characters in the sentence.

推荐答案

正如你所说,

字符串文字=Cámélan发现了红色的混合物,并且发现了它的存在感;

String text = "Cámélan discovered ônte red aleŕt \n Como se extingue la deuda";

所以每当你给出第一个输入时,接收第一个字符并进行比较。

So whenever you give first input, receive that first character and compare.

例如:如果你给Ca,那么

Eg: If you give Ca, then

if (StringUtils.isNotEmpty(substring)) { //this is the search text
substring=substring.substring(0,1); //now you get C alone.

}

所以不管你输入什么通过过滤第一个字符来显示。现在

So whatever you type it displays by filtering the first character. Now

 SpannableString builder = higlightString((yourContent.getText()), mHighlightColor);
    viewHolder.mTextView.setText(builder);




private SpannableString higlightString(String entireContent, int highlightColor) {
            SpannableString returnValue = new SpannableString(entireContent);

            String lowercaseNodeText = entireContent;
        try {
            Matcher matcher = mFilter.getPattern().matcher(((diacritical(lowercaseNodeText.toLowerCase()))));
            while (matcher.find()) {
                returnValue.setSpan(new ForegroundColorSpan(highlightColor), matcher.start(0),
                        matcher.end(0), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }

            return returnValue;

    }



 private String diacritical(String original) {
       String removed=null;
           String decomposed = Normalizer.normalize(original, Normalizer.Form.NFD);
           removed = decomposed.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
       return removed;
   }

测试用例:

当您输入Ca时,它会显示所有C内容获取所有数据并通过规范化内容过滤掉整个文本,并且它也与重音字符匹配并通过高亮显示。

When you give input Ca, it goes to the entire text by displaying all the C content get all the datas and filter out by normalising the content and it matches with accented characters too and display by higlighting.

这篇关于给定字符串中的混合重音和普通字符在搜索时在java中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆