Bue*_*ena 0 c# string integer winforms
我如何修剪和转换字符串如下:
string abc = "15k34"
int x = first two characters of abc // should be 15
but if abc begins with "0"
for example - string abc = "05k34"
int x = first two characters of abc // should be 5
Run Code Online (Sandbox Code Playgroud)
尝试使用以下代码:
string str = "15k34";
int val;
if (str.Length>1)
{
if (int.TryParse(str.Substring(0, 2), out val))
{
//val contains the integer value
}
}
Run Code Online (Sandbox Code Playgroud)