在比较字符串时忽略希伯来元音

use*_*860 4 java encoding hebrew

晚上好,我希望你能帮助我解决这个问题,因为我正在努力寻找解决方案.

我有一个单词的提供者,例如,他给了我发音的希伯来语单词 -

Vowelled - בַּיִתnot vowelled - בית

Vowelled - הַבַּיְתָהnot vowelled - הביתה

与我的提供者不同,我的用户通常不能输入希伯来语元音(我也不希望他这样做).用户故事是用户在提供的单词中搜索单词.问题是在元音和非元音词之间的比较.由于每个都由内存中的不同字节数组表示,因此equals方法返回false.

我试着研究UTF-8如何处理希伯来元音,看起来它只是普通字符.

我确实想向用户呈现元音,所以我想将字符串保持在内存中,但是在比较时我想忽略它们.有没有简单的方法来解决这个问题?

cho*_*ban 5

你可以使用Collat​​or.我无法告诉你它是如何工作的,因为它对我来说是新的,但这似乎可以解决问题:

public static void main( String[] args ) {
    String withVowels = "??????";
    String withoutVowels = "???";

    String withVowelsTwo = "??????????";
    String withoutVowelsTwo = "?????";

    System.out.println( "These two strings are " + (withVowels.equals( withoutVowels ) ? "" : "not ") + "equal" );
    System.out.println( "The second two strings are " + (withVowelsTwo.equals( withoutVowelsTwo ) ? "" : "not ") + "equal" );

    Collator collator = Collator.getInstance( new Locale( "he" ) );
    collator.setStrength( Collator.PRIMARY );

    System.out.println( collator.equals( withVowels, withoutVowels ) );
    System.out.println( collator.equals( withVowelsTwo, withoutVowelsTwo ) );
}
Run Code Online (Sandbox Code Playgroud)

从那里,我得到以下输出:

These two strings are not equal
The second two strings are not equal
true
true
Run Code Online (Sandbox Code Playgroud)