how could I trim and convert a string as following:
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
Try with following code:
string str = "15k34";
int val;
if (str.Length>1)
{
if (int.TryParse(str.Substring(0, 2), out val))
{
//val contains the integer value
}
}
string abc = "15k34";
int x = 0;
//abc = "05k34";
int val;
if (!string.IsNullOrEmpty(abc) && abc.Length > 1)
{
bool isNum = int.TryParse(str.Substring(0, 2), out val);
if (isNum)
{
x = val;
}
}
I think from the pseudocode you will typically have numbers with 'k' in them representing thousands.
So...
string abc = "15k34";
string[] numbers = abc.Split('k'); //This will return a array { "15", "34" }
int myInt = Convert.ToInt32(numbers[0]);
If the string was "05k34" the value of myInt would be 5 then.
documentation:
http://msdn.microsoft.com/en-us/library/1bwe3zdy
http://msdn.microsoft.com/en-us/library/bb397679.aspx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.