I need to split a string into two string variables at a specific length, more exactly after the first two characters.
Example 1: XX123456789 should be split into:
XX123456789Example 2: string NN125457878 should be split into:
NN125457878You can use String.Substring(Int32) and String.Substring(Int32, Int32) overloads like;
string s = "XX123456789";
string val1 = s.Substring(0, 2);
string val2 = s.Substring(2);
Console.WriteLine(val1);
Console.WriteLine(val2);
Prints;
XX
123456789
Here a demonstration.
You would use the Substring method.
For the first one, you would specify the start index of 0, with a length of 2. For the second one, you would use a start index of 2 and no length, which will return everything in the string from the third character until the end.
See the MSDN Documentation.
var theString = "XX123456789";
var val1 = theString.Substring(0, 2);
var val2 = theString.Substring(2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With