I've created a string using
String.Format(IPAddressRangeFormat, StartIP, EndIP);
Now I need to read the string back into the StartIP and EndIP objects.
What's the easiest way to do so in c#?
THanks, Li.
There is no easy way to do this as the reverse of a String.Format is not deterministic.
Both :
String.Format( "{0}{1}", "123", "456" )
String.Format( "{0}{1}", "12", "3456" )
gives you 123456. The machine won't just guess which one you want.
However there is a trickier way to do it, you do have regular expressions.
If you have :
String.Format ( "{0}-{1}", StartIP, EndIP);
You could use an expression :
var matches = Regex.Match ( String.Format ( "{0}-{1}", "127.0.0.1", "192.168.0.1"), "(?<startIP>.*)-(?<endIP>.*)" );
Console.WriteLine ( matches.Groups["startIP"].Value ); // 127.0.0.1
Console.WriteLine ( matches.Groups["endIP"].Value ); // 192.168.0.1
String.Split('-')?(?<startIP>.*)-(?<endIP>.*) is not a complicated regex and should not take any effort for a halfway competent coder to understand.
IPAddressRangeFormat? There's no general solution for this problem.