0

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

2
  • 1
    Why use LINQ and not string.Split? Commented Mar 16, 2012 at 10:13
  • my solution does not use Split() Commented Mar 16, 2012 at 10:52

6 Answers 6

1

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 Split at all). See InternalAdd() 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):

    private IEnumerable<string> BreakString(string source)
    {
        var delimiter = '/';
        var head = source.TakeWhile(c => c != delimiter);
        if (!head.Any())
        {
            yield break;
        }
        var tail = source.SkipWhile(c => c != delimiter)
            .Skip(1);
        yield return String.Join("", head);
        foreach (var t in BreakString(String.Join("", tail)))
        {
            yield return t;
        }

    }


// usage
from s in BreakString(source)

You can also go further and get rid of String.Join() by tail.Aggregate(new StringBuilder(), (sb, c) => sb.Append(c)).ToString();

Sign up to request clarification or add additional context in comments.

4 Comments

This seems like an awful lot of work just to split a string. :)
agree 100%, but its a good exercise for daily Kata :) Also internal implementation of String.Split may be complicated as well.
Well, nothing wrong with a mental exercise... though I'd be worried if the internal implementation of String.Split as overly complex :)
@theJoric, true... but most of that looks like edge-cases :)
1

You don't need LINQ for this:

string[] values = myString.Split(new []{'\\', '/'});

Introducing LINQ for such a task just adds complexity and reduces readability.

Comments

1

Sometimes, I think that LINQ is attempted to be used for everything, when it doesn't.

string[] data = myString.Split(new Char[]{'\\', '/'});

Comments

1

There's no need to use LinQ here, the default Split() method will do the work:

var path = "temp\\SimpleRec/bin/Debug/Geming.SimpleRec.vshost.exe"
var parts = path.Split(new[] { '/', '\\' });

Hope this helps!

Comments

1

You can split using LINQ by following way:

var list=from item in str.Split(new string[] { "\\","/" }, StringSplitOptions.None)
         select item;

2 Comments

LINQ makes no sense here. Result would be the same as str.Split(new [] { "\\","/" }, StringSplitOptions.None)
@the_joric: Wajih was just curious about the implemenation of LINQ in this scenario.....So,provide the solution with LINQ......
0

No LINQ required, simply do this:

var path = @"temp\\SimpleRec/bin/Debug/Geming.SimpleRec.vshost.exe";
var split = path.Split(new [] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.