1

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.

4
  • Can you please include more code. Having more context would help people understand your problem. Commented Sep 1, 2011 at 14:21
  • could you please post the content of your variables Commented Sep 1, 2011 at 14:22
  • 1
    What is the value of IPAddressRangeFormat? There's no general solution for this problem. Commented Sep 1, 2011 at 14:23
  • 1
    The code you posted does nothing, please post a complete sample that reproduces your problem setting. Commented Sep 1, 2011 at 14:23

4 Answers 4

4

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
Sign up to request clarification or add additional context in comments.

4 Comments

Using Regex is a bit of an over-complication in this case. why not String.Split('-')?
You could for the example I have given. However that will only work for a few limited situations. For the general situation of reversing a Format String, this is exactly what a Regex match is designed to do. Without knowing the exact circumstances of the OP, it is the only way to handle all possibilities.
Plus, once you get over your fear of Regexes they aren't that complicated. Or rather, you can have simple regexes and you can have complex ones - depending on what you are trying to do. IMHO (?<startIP>.*)-(?<endIP>.*) is not a complicated regex and should not take any effort for a halfway competent coder to understand.
Aaargh! I didn't mean that to be directed at you. Don't take it personally. I'll get my coat...
2

Probably the best way to have the separate type like IPRange where you will have 2 IP's in costructor and overriden ToString(). Th parse back you probably will need to implement custom logic in lets say static Parse(string) member.

Comments

0

The thing you want to do called Parsing and it can't be performed by String.Format method.

Comments

0

Depending on your formatting for IPAddressRangeFormat, use ToString() ...then string.Split(':') ...replacing the ':' with whatever char you are using.

I would really need to see your format for IPAddressRangeFormat to provide a better answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.