How can I split the following string on “\” and/or “/” using LINQ
I say and/or for “\” and “/” because in my file paths, I might end up getting forward slashes only, backslashes only or a mix of both.
"temp\\SimpleRec/bin/Debug/Geming.SimpleRec.vshost.exe"
"temp\\SimpleRec\\bin\\Debug\\Geming.SimpleRec.vshost.exe"
"temp/SimpleRec/bin/Debug/Geming.SimpleRec.vshost.exe"
Thanks
String.Split()seems to be the best option here, but just for fun it is also possible to do with LINQ.One of my implementations of String Calculator Kata uses this approach (no
Splitat all). SeeInternalAdd()method. I took functional approach with head and tail. You divide string to head and tail using.Take()and return head + recursive result of calling the same function for tail.Sample code for one char delimiter is below (things are getting complicated when delimiters are getting longer):
You can also go further and get rid of
String.Join()bytail.Aggregate(new StringBuilder(), (sb, c) => sb.Append(c)).ToString();