max
max

Reputation: 1665

Comparing String that had ampersand in flutter

I am trying to compare two string

"Food & Beverages" == "Food & Beverages"

and result I am getting is false. This is because the ascii value of the character 7 is different in & ASCII Number: 38

& ASCII Number: 65286

One value is from database and other is from file. is there a way to compare these two string?

in powershell they are asking to normalise the string.

How to compare strings that have an ampersand in them in PowerShell

Is there similar thing available in flutter?

Upvotes: 2

Views: 556

Answers (2)

Didier Prophete
Didier Prophete

Reputation: 2031

The easiest way is probably to remove these characters before you compare two string.

So something like:

String cleanString(String s) {
  return s.replaceAll(RegExp('[^a-zA-Z0-9 ]*'), '');
}

bool compare(String s1, String s2) {
  return cleanString(s1) == cleanString(s2);
}

Upvotes: 1

Toby
Toby

Reputation: 316

You should replace the characters before the comparison. I think this is the best way,

Upvotes: 1

Related Questions