Stephen Fischer
Stephen Fischer

Reputation: 2546

C# string.Split() Matching Both Slashes?

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!

More Code:

url = HttpContext.Current.Request.Path.Replace("http://", "");
string[] buffer = url.Split('/');

Turns out, Request.Path and Request.RawUrl are both changing my slashes, which is ridiculous. So, time to research that a bit more and figure out how to get the URL from a function that doesn't break my formatting. Thanks everyone for playing along with my insanity, sorry it was a misleading question!

Upvotes: 3

Views: 5523

Answers (6)

Charles Boyung
Charles Boyung

Reputation: 2481

those two functions are probably converting \ to / because \ is not a valid character in a URL (see Which characters make a URL invalid?). The browser (NOT C#, as you are inferring) is assuming that when you are using that invalid character, you mean /, so it is "fixing" it for you. If you want \ in your URL, you need to encode it first.

The browsers themselves are actually the ones that make that change in the request, even if it is behind the scenes. To verify this, just turn on fiddler and look at the URLs that are actually getting sent when you go to a URL like this. IE and Chrome actually change the \ to / in the URL field on the browser itself, FireFox doesn't, but the request goes through that way anyways.

Upvotes: 0

Jeffrey L Whitledge
Jeffrey L Whitledge

Reputation: 59443

I suspect (without seeing your whole application) that the problem lies in the semantics of path delimiters in URLs. It sounds like you are trying to attach a semantic value to backslashes within your application that is contrary to the way HTTP protocols define and use backslashes.

This is just a guess, of course.

The best way to solve this problem might be modifying the application to encode the path in some other way (such as "%5C" for backslashes, maybe?).

Upvotes: 0

Kiril
Kiril

Reputation: 40345

Update:
How about this:

Regex.Split(url, "/");

Upvotes: -1

Roger Lipscombe
Roger Lipscombe

Reputation: 91815

When I try the following:

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

... I get 4. Post more code.

Upvotes: 7

MK.
MK.

Reputation: 34517

Something else is happening, paste more code.

string str = "a\\b/c\\d";
string[] ts = str.Split('/');
foreach (string t in ts)
{
    Console.WriteLine(t);
}

outputs

a\b
c\d

just like it should. My guess is that you are converting / into \ somewhere.

Upvotes: 1

Catdirt
Catdirt

Reputation: 96

You could use regex to convert all \ slashes to a temp char, split on /, then regex the temp chars back to \. Pain in the butt, but one option.

Upvotes: 0

Related Questions