본문 바로가기
개발언어/C#

[C#] 문자,숫자,영어,한글 구분

by 창용이랑 2021. 11. 25.
728x90

 

bool IsKorean(char ch)
{
    if ((0xAC00 <= ch && ch <= 0xD7A3) || (0x3131 <= ch && ch <= 0x318E))
        return true;
    else
        return false;
}

 

bool IsEnglish(char ch)
{
    if ((0x61 <= ch && ch <= 0x7A) || (0x41 <= ch && ch <= 0x5A))
        return true;
    else
        return false;
}
 

bool IsNumeric(char ch)
{
    if (0x30 <= ch && ch <= 0x39)
        return true;
    else
        return false;
}

 

//허용하는 문자
bool IsAllowedCharacter(char ch, string allowedCharacters)
{
    return allowedCharacters.Contains<char>(ch);
} 

void CkeckString()
{
    string s = "한글ㄱㄴㅏㅓㄲㅌ힣abcDEF~!@#$%^&*()_+|-=\\{}[]'\";:,.<>/? ";
    string allowCharacters = "-_[]()"; 

    for (int i = 0; i < s.Length; i++)
    {
        if (IsKorean(s[i]) == true)
        {
            Print(s[i].ToString() + "  kor");
        }
        else if (IsEnglish(s[i]) == true)
        {
            Print(s[i].ToString() + "   eng");
        }
        else if (IsNumeric(s[i]) == true)
        {
            Print(s[i].ToString() + "   num");
        }
        else if (IsAllowedCharacter(s[i], allowCharacters) == true)
        {
            Print(s[i].ToString() + "   allow");
        }
        else
        {
            Print(s[i].ToString() + "   unknown-----------");
        }
    }
} 



출처: https://tystory.tistory.com/288 [최선을 다하여 사는 중]

출처 : https://tystory.tistory.com/288