C#에서 int.TryParse 메서드를 사용하여 숫자의 문자열 표현을 정수로 변환합니다. 문자열을 변환할 수 없는 경우 int.TryParse 메서드는 false, 즉 부울 값을 반환합니다.
숫자의 문자열 표현이 있다고 가정해 보겠습니다.
string myStr = "12";
이제 정수로 변환하려면 int.TryParse()를 사용하십시오. 변환되어 True를 반환합니다.
int.TryParse(myStr, out a);
예
using System.IO;
using System;
class Program {
static void Main() {
bool res;
int a;
string myStr = "12";
res = int.TryParse(myStr, out a);
Console.WriteLine("String is a numeric representation: "+res);
}
} 출력
String is a numeric representation: True