How to fold/normalize ligature characters in C# for string comparisons

I am exploring some of the more esoteric aspects of string comparisons at the moment (I went down a bit of a rabbit hole and there doesn’t seem to be an end in sight!).

I’d like to know how to compare a string containing characters with ligatures to a canonical non-ligature version (imagine an application for French language learning that lets the user type in oeuf or œuf interchangeably). I think this is called ‘folding’, but I could be wrong.

I’ve tried normalizing my strings using NFKD, which I thought would decompose the character into its constituent parts, but only some Unicode codepoints support decomposition. (Of course, my example character ‘œ’ doesn’t, which resulted in much hair-pulling.)

For example:

using System.Text;
using System.Globalization;

// This character does not support decomposition.
string str1 = "u0153"; // œ (LATIN SMALL LIGATURE OE)
string str2 = "oe";
string str1norm = str1.Normalize(NormalizationForm.FormKD);

Console.WriteLine(str1norm.IsNormalized()); // True
Console.WriteLine(str1norm.Equals(str2));   // False
Console.WriteLine(str1norm.Length);         // 1

// This character supports decomposition.
str1 = "uFB06"; // st (LATIN SMALL LIGATURE ST)
str2 = "st";
str1norm = str1.Normalize(NormalizationForm.FormKD);

Console.WriteLine(str1norm.IsNormalized()); // True
Console.WriteLine(str1norm.Equals(str2));   // True
// (Non-normalized comparison.) True in most locales, but not all (see below)
Console.WriteLine(str1.Equals(str2, StringComparison.CurrentCultureIgnoreCase)); 
Console.WriteLine(str1norm.Length);         // 2

References for the two Unicode characters:

  • https://www.compart.com/en/unicode/U+0153

  • https://www.compart.com/en/unicode/U+FB06

If only some ligatures are decomposable, I have a couple of questions:

  1. How do I determine this without manually checking through all the Unicode code points?
  2. Can I do a string comparison to a canonical version of the string without having to create a dictionary of all the characters and their decomposed forms? I mean, I like typing, but not that much.
  3. (Bonus round.) Why are some multi-character code points decomposable (like the ligature ‘st’ example), and some not? Is there something special about characters like ‘œ’? Who do I have to bribe to get these exceptions sorted out?

As a final illustration of my “what-on-earth-is-going-on?” mindset at the moment, ‘st’ is equivalent to ‘st’ without normalizing in 862 locales on my PC, with the string comparison failing in only 7, with those failure locales being:

  • aa (Afar language)
  • aa-DJ (Afar – Djibouti)
  • aa-ER (Afar – Eritrea)
  • aa-ET (Afar – Ethiopia)
  • en-US-POSIX (US English POSIX settings)
  • ssy (Saho language)
  • ssy-ER (Saho – Eritrea)

This is all very interesting, but the sheer apparent arbitrariness of it all is a bit overwhelming 🙂 As far as I know, Afar and Saho have Latin-based written forms, but I assume their Unicode code pages are less popular and may just lack the entries for ligatures which are present in others.

Finally finally, the single-character ‘st’ and its decomposed form ‘st’ are equal in the .NET Invariant culture, but only if case is ignored:

//...continuing from prior code...
Console.WriteLine(str1.Equals(str2, StringComparison.InvariantCulture));           // False
Console.WriteLine(str1.Equals(str2, StringComparison.InvariantCultureIgnoreCase)); // True

This is puzzling because both strings are lowercase.

(I know, “Just normalize your string and forget you ever saw this, Dave.” But I still think it’s interesting to know why this happens.)

Thanks for your time.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật