Home > Software design >  How to get length of Thai string in c#?
How to get length of Thai string in c#?

Time:04-05

I am facing issues while calculating length of strings that contain Thai characters. In the below image from Notepad , we need a way in c#/.NET to get the Document Length value (170) for the provided string -

Thai String Issue

String.Length works well for English Language/Characters, but for this example ("บ.อินเตอร์เทค สเปคเชียวตี้ กลาส จำกัด 28/10 หมู่ 1 ต. คลองอุดมชลจร") - it returns length as 69, instead of 170. Is there a way in c#/.NET to get the actual length of string values for non-English languages?

I tried using Encodings as well, but no luck. Any pointers/help on this will be great.

Thanks in advance!

CodePudding user response:

69 is correct, though.

บ.อินเตอร์เทค สเปคเชียวตี้ กลาส จำกัด 28/10 หมู่ 1 ต. คลองอุดมชลจร contains 69 characters; the UTF-8 encoding of it is 170 bytes long.

Notepad is showing you the length of the encoded content.

If you do need the encoded length, use Encoding.UTF8.GetByteCount().

  • Related