Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find out if my string contains the "micro" Unicode character?

Tags:

c#

unicode

I have a Excel Spreadsheet with lab data which looks like this:

µg/L (ppb)

I want to test for the presence of the Greek letter "µ" and if found I need to do something special.

Normally, I would write something like this:

if ( cell.StartsWith(matchSequence) ) { 
//.. <-- universal symbol for "magic" :)
}

I know there is an Encoding API in the Framework, but should I use it for just this one edge-case or just copy the Greek micro symbol from the character map?

How would I test for the presence of a this unicode character? The character map seems like a "cheap" fix that will bite me later (I work for a company which is multinational).

I want to do something that is maintainable and not just some crazy math-voodoo conversion that only works for this edge case.

I guess I'm asking for best practice advice here.

Thanks!

like image 634
Chris Avatar asked Nov 29 '22 06:11

Chris


1 Answers

You need to work out the unicode character you're interested in, then you can represent it with in code with an escape sequence.

For example, µ is U+00B5, so you just need:

if (text.Contains("\u00b5"))

You can find out the Unicode value from charmap or from the Unicode code charts.

like image 57
Jon Skeet Avatar answered May 10 '23 06:05

Jon Skeet