Hesein Burg Hesein Burg - 9 months ago 35
C# Question

How to split/extract complex string data c#

In this scenario the data I could have in my string may look like below but keep in mind the ids are dynamically generated so this isn't static and could be more than 2 if you haven't caught onto that.


info:server blah blah, you. 2019,;:10-!ws.csd
1. id=value, otherid=value, pos=(22,22,33)
2. id=value2, otherid=value2, pos=(24,21,33)
info other info info info info etc etc.


How would I go about extracting sets of values like the bold portion within in the above block quote?

I have tried using String.Split but it doesn't seem to be advanced enough for my scenario and am not sure how to write this with Regex.Split

The data I am getting will always be returned with that structure but I may get other junk as well, but the data I want is returned.

End game is to store this in a dictionary with a class object of properties as the value.

Answer Source

You can do with Substring since you have already way of expecting when to start and when to end on your searching.

string result = x.Substring(x.IndexOf("csd"), (x.IndexOf("info ") - x.IndexOf("csd")));

I start searching on the start of the word "csd" and ends with the word "info " (with space), since there is also a word of info at the beginning of your string.

The result would be:

"csd 1. id=value, otherid=value, pos=(22,22,33) 2. id=value2, otherid=value2, pos=(24,21,33) "