C # string.Split () Matching both cuts?

I have a .NET 3.5 web application written in C # that does some URL processing, including the file path, and I am having a problem. When I call string.Split('/'), it matches the characters "/" and "\". Is this ... supposed to happen? I assumed that he would have noticed that the ASCII values ​​were different and missed it, but it seems like I'm wrong.

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

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

Has anyone come across this before? Simple answer? I appreciate it!

Details Code:

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

It turns out that Request.Path and Request.RawUrl are changing my slashes, which is ridiculous. So, the time to explore this is a little 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, that was a misleading question!

+3
6

:

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

... 4. .

+7

- , .

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

a\b
c\d

, . , /\-.

+1

regex \slashes temp char, /, \. , .

0

( ), URL-. , , , HTTP .

, .

- (, "% 5C" , ?).

0

, , \/, \ URL- (. URL ?). (NOT #, ) , , , /, "" . \ URL-, .

The browsers themselves are actually the ones that make this change in the request, even if it is behind the scenes. To verify this, simply turn on the violinist and look at the URLs that are actually sent when you go to a URL like this. IE and Chrome actually change the \ in / field in the URL in the browser itself, FireFox does not, but the request goes through this path anyway.

0
source

Update:
How about this:

Regex.Split(url, "/");
-1
source

Source: https://habr.com/ru/post/1739883/


All Articles