Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect the existence of non Latin characters in a string?

Tags:

c#

I want to change all filenames containing non Latin characters to a random unique Latin string. But how can I detect the existence of non Latin character in the original filenames?

Edit

The non-latin characters might be chinese, japanese, korean, arabic, umlaut, etc characters.

like image 810
kiss my armpit Avatar asked Oct 29 '25 02:10

kiss my armpit


1 Answers

You could use regex:

if(Regex.IsMatch(input, "[^a-zA-Z]"))
{
    // non-latin found
}

It will work for every letter different then a to z and A to Z.

If you'd like to allow digits too, use following: [^a-zA-Z0-9].

Non-regex solution

You could use LINQ as well, because string implements IEnumerable<char>:

if(input.ToLower().Any(c => c <= 'a' || c >= 'z'))
{
    // non-latin found
}
like image 51
MarcinJuraszek Avatar answered Oct 31 '25 14:10

MarcinJuraszek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!