SoFunction
Updated on 2025-03-07

C# development int and string conversion operations

1、int–>string

 int a = 15;
 string s1 = ();
 string s2 = (a);

2、string –>int

 string s = "18";
 int a1 = (s);
 int a2;
 (s, out a2);
 int a3 = Convert.ToInt32(s);

Summarize:

1. You can use Convert to convert int and string back and forth, and you can specify the conversion card;

2. Convert to string, you can use the ToString method;

3. Convert to int, can be used or method.

Why not have a method? No, ToString is OK.

Supplementary knowledge: In C#, convert int to string and string to int

1. Int convert to string

Use toString

Or () as follows

For example:

int varInt = 1;
string varString = (varInt);
string varString2 = ();

2. Convert string to int

If you are sure that the string is a character that can be converted into a number, you can use (string s), and the statement returns the int value obtained by the conversion;

If you cannot determine whether the string can be converted to a number, you can use (string s, out int result), which returns a bool value, indicating whether the conversion operation is successful. The parameter result is a variable that stores the conversion result.

For example:

string str = ;
str = "123";
int result=(str);
string str = ;
str = "xyz";
int result;
(str, out result);

C# —— String type conversion Int type, Float type

int a;
float b;
a =  (); //String type converts int typeb = (); // string type convert float type

And so on\(^o^)/~

The above article on C# development int and string conversion operations is all the content I share with you. I hope you can give you a reference and I hope you can support me more.