Convert string to int using C #

I am using C # and I want to convert a string to int to check the name. For example, **or is 12not a name. I just want to convert the string to ASCII values ​​and then check the name. How to do it?

+3
source share
3 answers

Converting back and forth is simple:

int i = int.Parse("42");
string s = i.ToString();

If you do not know that the input line is valid, use the method int.TryParse().

+2
source

, , ? , , , , , . , , :

public bool IsValidName(string theString)
{
    for (int i = 0; i < theString.Length - 1; i++)
    {
        if (!char.IsLetter(theString[i]) && !char.IsWhiteSpace(theString[i]))
        {
            return false;
        }
    }
    return true;
}

, , , , , . (, .)

+1

, , ASCII :

System.Text.Encoding.ASCII.GetBytes(str)
-1

Source: https://habr.com/ru/post/1751702/


All Articles