开发者

How to convert string into int?

开发者 https://www.devze.com 2022-12-16 03:57 出处:网络
i have a doubt. May i know how to convert string to int.i know using parse we can do开发者_JAVA百科 it. instead of parsing is there any thing to convert.Well, no.

i have a doubt. May i know how to convert string to int.i know using parse we can do开发者_JAVA百科 it. instead of parsing is there any thing to convert.


Well, no.

You can call

int k = Convert.ToInt32("32");

But it still parses it.

-- Edit:

For completeness, here is the code to do it without 'framework' functions:

    public static int ToInt32 (string s)
    {
        int result = 0;

        foreach(char c in s){
            if( c >= '0' && c <= '9' ){
                result = (result * 10) + (c - '0');
            }
        }

        if( s[0] == '-' ){
            result = -result;
        }

        return result;
    }


Are you wanting to get the numerical value of the characters in the string? If so, you could cast individual characters to int to grab the unicode numbers. Other than that, it doesn't make much sense to not use int.Parse or int.TryParse.

public void PrintValues(string aString)
{
    foreach(char c in aString)
    {
        int x = (int)c;
        Console.WriteLine(x);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消