0

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

3
  • You could cast the string to an int. Show some code please, for people to understand why Parse or TryParse wouldn't be of use. Commented Jan 11, 2010 at 6:38
  • @silky: You are right. That is really stupid of me to write that. Commented Jan 11, 2010 at 6:56
  • shahkalpesh: Hey, we all do stupid things sometimes :) Commented Jan 11, 2010 at 7:03

2 Answers 2

4

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;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Mr. Silky is there any thing i know this. thank you for response
Thank you for response Mr. silky
0

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);
    }
}

3 Comments

I have a doubt Mr. Nate. you code is super is it concept of boxing and unboxing
No, that has nothing to do with boxing/unboxing.
ok thank you Mr. Jon skeet i understood thank you for response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.