2

Trying to compare two arabic strings, one with diacritic marks and one without.

بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ and بسم الله الرحمن الرحيم

Is there anyway either to remove the diacritic marks and then compare or just compare characters and ignore the diacritic marks?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • possible duplicate https://stackoverflow.com/questions/4418997/how-to-compare-a-to-%C3%81-on-the-iphone – Shehata Gamal May 10 '19 at 20:53
  • @Sh_Kkan I think it's useful to have an Arabic-specific question, as diacritics in Arabic script don't behave the same as accents in Latin-based languages in some cases. – Flimm Feb 23 '22 at 13:46

1 Answers1

2

You can compare() the strings with the .diacriticInsensitive option:

import Foundation

let s1 = "بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ"
let s2 = "بسم الله الرحمن الرحيم"

print(s1 == s2)
// false

print(s1.compare(s2, options: .diacriticInsensitive) == .orderedSame)
// true
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382