How to write a search text implementation in Java
Asked Answered
A

1

0

I am trying to write a search text implementation in Java

With the below code below:

public String log(Object... source) {...//simple System.out.println for debugging purposes}
public boolean searchInLoweCase(CharSequence searchTag, CharSequence containsTag) {
   var contains = containsTag.toString().contains(searchTag);
   log("search", "searchTag", searchTag, contains, "containsTag", containsTag);
   IntStream.range(0, searchTag.length()).forEachOrdered(charIdx -> {
       log("search",
        "searchTag[" + charIdx + "].charAt/codePointAt/int", searchTag.charAt(charIdx), searchTag.codePointAt(charIdx), "as", (int) searchTag.charAt(charIdx),
        "containsTag[" + charIdx + "].charAt/codePointAt/int", containsTag.charAt(charIdx), searchTag.codePointAt(charIdx), "as", (int) containsTag.charAt(charIdx)
       );
   });
   return contains;
 }

For input "i":
enter image description here

the output is

{searchTag, i, true, containsTag, i̇letişim}
{searchTag[0].charAt/codePointAt/int, i, 105, as, 105, containsTag[0].charAt/codePointAt/int, i, 105, as, 105}

For input "il":
enter image description here

the output is

 {searchTag, il, false, containsTag, i̇letişim}
 {searchTag[0].charAt/codePointAt/int, i, 105, as, 105, containsTag[0].charAt/codePointAt/int, i, 105, as, 105}
 {searchTag[1].charAt/codePointAt/int, l, 108, as, 108, containsTag[1].charAt/codePointAt/int, ̇, 108, as, 775}

Here you can see the hidden character there.
I tried to remove the hidden character with answer here;
however it did not work.

Did you exprienced it before?

Antung answered 18/4, 2022 at 8:42 Comment(2)
Don’t use chars, use code points.Tibold
@Boris the Spider. After using the code points, i could see the same numbers. I will try to implement search function with that. Thanks.Antung
A
0

Implementation
To search strings, one can use equals or contains after uppercasing them with locale tuned.

The Bug:
However, I figured that there is a bug in JDK and Javascript for turkish and german character convertions.

JavaScript:
In JavaScript, one should detect the locale first:

var lng = navigator.language; //tr

According to this answer, one can use below code to uppercase strings in native javascript.

"iıöçşğü".toLocaleUpperCase('tr-TR'); //İIÖÇŞĞÜ

JAVA | GWT:
In Java, one should detect the locale first: (like here)

Locale.getDefault().getLanguage().equals("tr");

Or in Java GWT, one should detect the locale first: (like here)

LocaleInfo.getCurrentLocale().getLocaleName().contains("tr");

Then, according to language detected, If it is an unsupported language, he should use the below fix: (like here)

"iıöçşğü".toUpperCase(Locale.ROOT); //İIÖÇŞĞÜ
Antung answered 18/4, 2022 at 12:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.