C# store all strings between strings
As an Idea, you could try Regexp, but faster solution would be to parse file yourself like this:
using System; using System.IO; namespace HelloWorld { class Hello { static void Main() { try { string text = File.ReadAllText("data.file"); int spos = -1; int epos; while((spos = text.IndexOf("point [", spos + 1)) != -1){ epos = text.IndexOf("]", spos); if(epos != -1){ Console.WriteLine("Found: " + text.Substring(spos, epos - spos + 1)); spos = epos + 1; } else { Console.WriteLine("No ending bracket found."); break; } } } catch(Exception e){ Console.WriteLine(e.Message); } } } }
Try it here: https://repl.it/GRDS/2
Code searches for "point [" and then from the position it found it (you could add +7 so it would skip that string it just found) it looks for "]"
When it find "]" it gets substring from the posittion it found "point [" on to the position it found "]" on. And then loop starts again from the position it found "]" at.
Thet being said, you would need to make sure yourself if parsed value is valid, if file would missing closing bracket of point it would parse until closest closing bracket, so the resulting data would be much more then bunch of numbers. Regex can skip such invalid point but it is probably better to know if such point exists and not try to parse file further. If the file is generated then possibility of it being invalid is small.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now