Split and cut string in C# [closed]

Your string looks like it’s being broken down by sections that are separated by a ,, so the first thing I’d do is break it down into those sections

string[] sections = str.Split(',');

IT looks like those sections are broken down into a parameter name, and a parameter value, which are seperated by :. so I’d iterate over your sections, and break that down into name and value

string name = sections[i].Split(':')[0];
string value = sections[i].Split(':')[1];

now your value has a little bit more than just an IP address, so you can use a regular expression to extract the first thing that looks like an ip address

var address = Regex.Match(value, @"\d+\.\d+\.\d+\.\d+").Value;

You can also make use of the name string to determine which sections represent Src and which ones represent Dst


Now be careful with this. That regex that I gave you will match invalid IPs such as 300.19028.38.1 You can put a bit more effort than I did into getting a better regex if you’re afraid of matching things that are not IP addresses.

Also, if you have extra unexpeced , or : in your string, then this logic is rendered invalid.

Browse More Popular Posts

Leave a Comment