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.
2 Answers
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;
}
2 Comments
Surya sasidhar
Mr. Silky is there any thing i know this. thank you for response
Surya sasidhar
Thank you for response Mr. silky
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
Surya sasidhar
I have a doubt Mr. Nate. you code is super is it concept of boxing and unboxing
Jon Skeet
No, that has nothing to do with boxing/unboxing.
Surya sasidhar
ok thank you Mr. Jon skeet i understood thank you for response
ParseorTryParsewouldn't be of use.