C# string.Split() Matching Both Slashes?

Posted by Sheep Slapper on Stack Overflow See other posts from Stack Overflow or by Sheep Slapper
Published on 2010-04-05T18:20:12Z Indexed on 2010/04/05 18:23 UTC
Read the original article Hit count: 339

Filed under:
|
|
|
|

I've got a .NET 3.5 web application written in C# doing some URL rewriting that includes a file path, and I'm running into a problem. When I call string.Split('/') it matches both '/' and '\' characters. Is that... supposed to happen? I assumed that it would notice that the ASCII values were different and skip it, but it appears that I'm wrong.

// url = 'someserver.com/user/token/files\subdir\file.jpg
string[] buffer = url.Split('/');

The above code gives a string[] with 6 elements in it... which seems counter intuitive. Is there a way to force Split() to match ONLY the forward slash? Right now I'm lucky, since the offending slashes are at the end of the URL, I can just concatenate the rest of the elements in the string[], but it's a lot of work for what we're doing, and not a great solution to the underlying problem.

Anyone run into this before? Have a simple answer? I appreciate it!

© Stack Overflow or respective owner

Related posts about c#

Related posts about split