7

Is there any way I can find a Unicode character's bidirectional character type in C#?

I want to look through the characters in a string and decide if they are all strong LTR, strong RTL, a mixture of strong LTR and neutral, etc.

friedemann_bach
  • 1,418
  • 14
  • 29
Bernard Darnton
  • 338
  • 2
  • 11
  • Maby this will help you: http://stackoverflow.com/questions/4330951/how-to-detect-whether-a-character-belongs-to-a-right-to-left-language – HABJAN Aug 31 '11 at 09:46

1 Answers1

6

System.Globalization.CharUnicodeInfo.GetBidiCategory(ch) is your friend.

The problem is that the function is internal. This MichKap (RIP) blog post shows you how to call it using reflection.

Serge Wautier
  • 21,494
  • 13
  • 69
  • 110
  • You can get exactly the same information with [char structure's](http://msdn.microsoft.com/en-us/library/system.char.aspx) static methods. The problem is, none of them will give you the directionality. – Paweł Dyda Aug 31 '11 at 16:17
  • OK, it seems that I am a bit wrong as the char methods will give you Unicode category and not BiDi category. Somehow they don't think this information is useful. – Paweł Dyda Aug 31 '11 at 16:23
  • I also thought I would find it in `Char` as well ;-) – Serge Wautier Aug 31 '11 at 19:14
  • Thanks, Serge. That blog post is spot on. – Bernard Darnton Aug 31 '11 at 22:05
  • This doesn't work on Android. For some reason it can't find the GetBidiCategory function when compiled in Release, in Debug it finds it. – Niro Jun 10 '21 at 16:30