Scar Scar - 1 year ago 106
C# Question

How do I split a String into 2 different types?

I have a string "201607" and I need to split it to 2 separate types. 2016 into int and 07 into byte. I have seen string split functions which all use delimeters but that won't work here. Is there an easier way to do this or so I have to split it into chars and then reconstruct them in C#?

Answer Source

Try this Example

string input="201607";
int integerPart=0;      
if(int.TryParse(input.Substring(0,4),out integerPart))
    Console.WriteLine("Integer value is {0}",integerPart);
    Console.WriteLine("Conversion Failed");
byte bytePart = byte.Parse(input.Substring(4));
Console.WriteLine("Byte Part is {0}",bytePart);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download