java将字符串与英语以外的语言进行比较

java将字符串与英语以外的语言进行比较,java,string,localization,Java,String,Localization,我正在构建一个支持阿拉伯语和英语的应用程序 我有一个列表,我希望用户输入一个字符串,这样我就可以找到列表中是否存在他的字符串 我用这个: String userstring = bla bla bla; for (int i = 0; i < allFoods.size(); i++) { if (allFoods.get(i).toLowerCase().contains(userstring.toLowerCase())) //do s

我正在构建一个支持阿拉伯语和英语的应用程序

我有一个列表,我希望用户输入一个字符串,这样我就可以找到列表中是否存在他的字符串

我用这个:

String userstring = bla bla bla;

for (int i = 0; i < allFoods.size(); i++) {
    if (allFoods.get(i).toLowerCase().contains(userstring.toLowerCase()))
                    //do something here
            }
stringuserstring=blablablabla;
对于(int i=0;i
当用户输入的单词是英语时,该代码可以完美地工作。但当用户输入阿拉伯字符串时,我没有得到任何结果

请问我做错了什么?我该怎么办

谢谢你


编辑:我不想对字符串进行排序和比较,但我想检查是否相等(包含)如果要进行字符串比较,可以使用
Collator
API:

List<String> list = ...;

// create collator for arabic
Collator collator = Collator.getInstance(new Locale("ar"));
collator.setDecomposition(Collator.FULL_DECOMPOSITION);
collator.setStrength(Collator.SECONDARY); // ignores lower/upper case

// sort list
Collections.sort(list, collator);
// or use it as any other comparator
List=。。。;
//为阿拉伯语创建拼贴符
Collator-Collator=Collator.getInstance(新语言环境(“ar”);
collator.setDecomposition(collator.FULL_DECOMPOSITION);
缩孔器固定强度(缩孔器次级);//忽略小写/大写
//排序表
集合。排序(列表、排序器);
//或者将其用作任何其他比较器

我不知道这个API是否可以用来测试一个字符串是否包含在另一个字符串中。

在比较之前,将字符串字符集转换为ISO-8859-6(阿拉伯语):

在java中转换字符集:

Charset utf8charset = Charset.forName("UTF-8");
Charset iso88596charset = Charset.forName("ISO-8859-6");

ByteBuffer inputBuffer = ByteBuffer.wrap(new byte[]{(byte)0xC3, (byte)0xA2});

// decode UTF-8
CharBuffer data = utf8charset.decode(inputBuffer);

// encode ISO-8859-6
ByteBuffer outputBuffer = iso88596charset.encode(data);
byte[] outputData = outputBuffer.array();

代码已完成。

您的问题是使用小写。即使utf-8似乎解决了基本的比较问题,但当涉及到使字符串小写时,java自然会感到困惑,因为它不知道如何使字母小写。例如,在土耳其语中,“I”的小写字母是“ı”而不是“I”,因此

首先用java-Dfile.encoding=UTF-8启动应用程序。。。这是一个常见的错误,在没有utf-8编码的情况下运行应用程序

这是我的解决方案;我添加所有所需的区域设置,然后对每个区域设置进行测试

public class MultiLanguageComparator {


    Set<Locale> localeList = new HashSet<Locale>();

    public MultiLanguageComparator() {
        localeList.add(Locale.getDefault());
        localeList.add(Locale.ENGLISH);
    }

    public MultiLanguageComparator(String localePrefix) {
        this();
        Locale[] locales = Locale.getAvailableLocales();
        localePrefix = localePrefix.toLowerCase(Locale.ENGLISH);
        for (Locale l : locales) {
            if (l.toLanguageTag().startsWith(localePrefix)) {
                localeList.add(l);
            }
        }
    }

    /**
     * if s1 contains s2 returns true
     *
     * @param s1
     * @param s2
     * @return
     */
    public boolean contain(String s1, String s2) {
        for (Locale locale : localeList) {
            String tmp1 = s1.toLowerCase(locale);
            String tmp2 = s2.toLowerCase(locale);
            if (tmp1.contains(tmp2)) return true;
        }
        return false;
    }

    public static void main(String[] args) {

        Locale[] locales = Locale.getAvailableLocales();

        String s1 = ....
        String s2 = ....
        MultiLanguageComparator comparator = new MultiLanguageComparator("ar"); // as you want to add arabic locales, I suppose all of them or you may just add ar-sa for suudi arabia locale
        System.out.println(comparator.contain(s1, s2));

    }
}
公共类多语言比较器{
Set localeList=new HashSet();
公共多语言比较器(){
添加(Locale.getDefault());
localeList.add(Locale.ENGLISH);
}
公共多语言比较器(字符串localePrefix){
这个();
Locale[]locales=Locale.getAvailableLocales();
localePrefix=localePrefix.toLowerCase(Locale.ENGLISH);
for(语言环境l:语言环境){
if(l.toLanguageTag().startsWith(localePrefix)){
localeList.add(l);
}
}
}
/**
*如果s1包含s2,则返回true
*
*@param s1
*@param s2
*@返回
*/
公共布尔包含(字符串s1、字符串s2){
for(区域设置:localeList){
字符串tmp1=s1.toLowerCase(locale);
字符串tmp2=s2.toLowerCase(locale);
if(tmp1.contains(tmp2))返回true;
}
返回false;
}
公共静态void main(字符串[]args){
Locale[]locales=Locale.getAvailableLocales();
字符串s1=。。。。
字符串s2=。。。。
MultiLanguageComparator comparator=新的MultiLanguageComparator(“ar”);//当您想要添加阿拉伯语语言环境时,我想所有这些语言环境或者您可以为suudi arabia语言环境添加ar sa
系统输出println(比较器包含(s1,s2));
}
}

我在比较德语字符串和umlaut时遇到问题。我使用了Unicode转义,它解决了我的问题。你可以找到清单

我直接在字符串中使用了Unicode转义

String mystring = "GERÄT";
mystring.equals("GER\u00C4T");

您是否尝试过将字符集转换为UTF-8?(或其他字符集)@RussellGutierrez不,我以前没有这个想法我想你的列表中的所有食物都包含阿拉伯字符串?@TomJonckheere是的,你说得对。@MarcoDinatsoli比较两个对象会检查它们的相等性。我不想比较和存储字符串,我想检查相等性
s1.equals(s2)
s1相同。compareTo(s2)==0
collator。compare(s1,s2)==0
。这(几乎)给出了我不想要的精确匹配。我需要这个容器。我希望你明白我的意思,现在我明白了。但正如我在上面所写的:我不知道这个API是否可以用来测试一个字符串是否包含在另一个字符串中。其次,请不要只是从其他问题中复制代码。第三,我应该转换的字符串在哪里。第四,字符串结果在哪里?