How to convert strings to int in C#

3 Answers

0 votes
using System;

class Program
{
    static void Main()
    {
        string s = "981";

        int n = int.Parse(s);
        
        Console.WriteLine(n);
    }
}

 
 
/*
run:
     
981
 
*/

 



answered Sep 13, 2020 by avibootz
0 votes
using System;

class Program
{
    static void Main()
    {
        string s = "981";
        int n;
        
        if (!int.TryParse(s, out n)) {
            Console.WriteLine("s is not a number");
        }
        else {
            Console.WriteLine(n);
        }
    }
}

 
 
/*
run:
     
981
 
*/

 



answered Sep 13, 2020 by avibootz
0 votes
using System;

class Program
{
    static void Main()
    {
        string s = "9813";
        
        int n = Convert.ToInt32(s);
        
        Console.WriteLine(n);
    }
}


 
 
/*
run:
     
9813
 
*/

 



answered Sep 13, 2020 by avibootz

Related questions

2 answers 132 views
1 answer 106 views
1 answer 155 views
1 answer 107 views
107 views asked Jan 23, 2024 by avibootz
1 answer 109 views
1 answer 99 views
99 views asked Aug 27, 2023 by avibootz
1 answer 97 views
...