Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# String Split Between Dividers

Maybe I misunderstood the title, sorry.
I have a string;

This is a test string. It was opened on %Date% for the customer named
%Customer%.

Here I want to be able to get a string containing "Date" and "Customer" with a method. How do I get the parts between the special characters "%" as strings?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Divider character and string can change dynamically. Thanks in advance.

>Solution :

You could extract it with System.Text.RegularExpressions.RexEx()

%[^%]*%" – starts with %, ends with % but doesn’t contain it [^%]*

string str = "This is a test string. It was opened on %Date% for the customer named %Customer%.";
string[] res = Regex.Matches(str, "%[^%]*%").Cast<Match>().Select(x => x.Value.Trim('%')).ToArray(); 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading