0

I need to compare each word in the recognized String with the base string. In logcat, I see the two strings are the same but when compared, it returns false. Which step did I go wrong?

This is my code

String check ='HĐ:';
   for(String word in textInLine.split(" ")){

      log("CHECK_WORD_TEXT ${word} AND ${check}");
      log("CHECK_WORD_COMPARE ${word == check}");

      }

And this is Logcat result

[log] CHECK_WORD_TEXT HÐ: AND HĐ:
[log] CHECK_WORD_COMPARE false

I tried with a.contains(b) and a.compare(b)==0 but both returned false results

1
  • Comparing Unicode strings can be tricky. Two strings could appear identical but could be represented in different ways (precomposed or decomposed). Additionally the string could contain zero-width whitespace embedded in it. Try printing word.runes and check.runes. Commented Jan 1, 2024 at 0:22

1 Answer 1

1

you can try checking the string word length to see what's going in here i think one of those words you are comparing together has an extra whitespace maybe you can try using trim() function on string to remove extra white spaces on right and left of the string

also if the comparison is not case senestive you can use toLowerCase() function to ensure that both are same letters

word.toLowerCase().trim() == check.toLowerCase().trim()

i hope that helps you

also there is a builtin function in flutter called identical() that takes two parameters as strings and see if they are identical in everything

This is an optimization, as two strings with the same characters in the same order can be the same object. that's the functionality of identical() function

Edit: after seeing your updated question i see that check string is the one which has an extra whitespace at the end maybe the word doesn't have that extra white space at end so they are not equal

Another solution

import 'package:intl/intl.dart';

void main() {
  String string1 = "HÐ";
  String string2 = "HĐ";

  // Normalize the strings using NFC (Normalization Form Canonical Composition)
  String normalizedString1 = normalize(string1);
  String normalizedString2 = normalize(string2);

  if (normalizedString1 == normalizedString2) {
    print("Strings are equal");
  } else {
    print("Strings are not equal");
  }
}

String normalize(String input) {
  return Normalizer.normalize(input, NormalizerForm.NFC);
}
Sign up to request clarification or add additional context in comments.

5 Comments

i added word.toLowerCase().trim() == check.toLowerCase().trim() but still false :< Do you have any other way?
@SơnVi ok i can see you problem here, in actually your issue is with string encoding as HÐ: not a utf-8 encoded , so it mean use HD: AND HD: not HÐ: AND HĐ:, you can actually see the diffrence here and noticed it
@SơnVi it's maybe because of your keyboard language based on your country, so that's why encoding may be the problem here
hope that works for you
@SơnVi see my updated answer for extra approach to normalize your strings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.