S
S
Sergey Panov2018-06-10 14:50:44
C++ / C#
Sergey Panov, 2018-06-10 14:50:44

How to determine the language in a string?

Good afternoon. There is a string of 4-9 characters.
I need to understand what language this word is in. Either Russian or English.
Can you please tell me how to implement a string check for language belonging?
I have an idea, cut the string to one character, check this character with the ASCII code, and if the code matches the letter, then determine. But I got up on the problem of translating a string from one letter into a symbol. Only the ToCharArray() method is shown.
Can you please tell me how to implement this correctly? Thanks in advance .

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
Oleg Pogrebnyak, 2018-06-10
@select8

You can get the first character with [0].
And you can just check:
if (myString[0] >= 'a' && 'z' <= myString[0])

N
Nikita, 2018-06-10
@vasilek-nik

Using regular expressions

if (!Regex.IsMatch(stringToCheck, @"\P{IsCyrillic}"))
{
    // Сработает если все символы - кириллица
}
if (!Regex.IsMatch(stringToCheck, @"\p{IsCyrillic}"))
{
    // Сработает если хотя бы один символ - кириллица
}

Checking for Latin can be done using a similar 'IsBasicLatin' block.
You can learn more here
If without regular expressions, then you can, for example, compose a constant string - the Russian alphabet.
After checking whether the desired character is included in the alphabet string, for example, using the Contains function of the String class.
PS the question is rather general, in C#, so I think the Unity tag should be removed.

G
Griboks, 2018-06-10
@Griboks

Cyrillic, if my memory serves me right, is encoded in 8 bits, and Latin - 7. So, the code of the Russian letter> 127. Then you can google how to greet one type to another in sharpe.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question