[Slash and backslash in asp.net]c#

Source: Internet
Author: User

Introduction

In the field of business, to customer deployment projects, three projects to be integrated together, which results in different data formats, the path of the slash and backslash caused a lot of problems.

Check out this information, here to do some records, is a summary of it.

forward slash (/) versus backslash (\) Summary

Forward slash/denote division, delimited. Typically used in Windows systems to separate command-line arguments,/indicate options, and so on. cannot be a file name.

Backslash \, used in Windows system to represent the directory.

In the UNIX system, the/represents the directory. Because the web follows UNIX naming, in the URL (URL),/represents the directory.

  C # Mid- slash \ with Slash / Transfer character Problems with Paths

\ is an escape character in C #, as shown below

  

Escape sequences

Character

\’

Single quotation marks

\”

Double quotes

\\

Back slash

/

Empty

\a

Warning

\b

Backspace

\f

Page change

\ n

Line break

\ r

Enter

\ t

Horizontal tab

\v

Vertical tab

For example, to output this section of the string "/_\" you might think it's written like this @ "/_\"

But this compiler does not let you pass, but as the table shows, you can write "\"/_\\\ ""

In addition, many people like to use D:\test.txt when getting the file path.

And then there's the so-called escape character error.

The solution to the general people is to use @d:\test.txt I don't know if this would bring some problems.

But what I've been doing is that d:/test.txt can also be fully accessible

Found on the internet, it seems that the biggest difference between the slash and the backslash is

In a network, you cannot use slashes in Windows systems to separate command-line arguments,/express options, and so on.

Arithmetic operator/used to fetch a quotient such as 5/2=2.5

Reference Description: There is a forward slash and backslash, forward slash, is generally called the slash, the symbol is "/"; the backslash's symbol is "\" reference slash (/) there is no special meaning in Java, that is, to represent a character '/';
backslash (\) Otherwise, it and the character immediately following it form an escape character, such as "\ n" (for line break), "\" "(for the character '"), etc., so in the string to represent the character ' \ ' to be represented by "\ \", for example: If you define a string such as "name\ Sex "is the wrong way to define string s =" Name\\sex ";
Note: the "\ \" representation in a regular expression and the character immediately following it form an escape character (let's name it first) and represent a special meaning; so if you want to represent a backslash in a regular expression, you should write "\\\\". If you do so get a matcher,matcher m = pattern.compile ("\ \"). Matcher ("\ \") will give an error, and you should write Matcher m = pattern.compile ("\\\\"). Matcher ("\ \") is the correct and matching reference next we look at the replace (charsequence target,charsequence Replacement) method in the String class with ReplaceAll ( The difference between a string regex, String replacement) method:

1  Public Static voidMain (string[] arg)2Throws Ognlexception {String s ="Sdf\\a\\aa";3 //replace the backslash in S with \ \4System. out. println (s);5System. out. println (S.replaceall ("\\\\","\\\\\\\\"));6System. out. println (S.replace ("\\","\\\\"));}

The reference shows that both of the above returns the same substitution result.
The key here is that String.replaceall () is used as a parameter with regular expression. However, the string for the Java itself has similar handling for the escape character \. First, Java interprets "\\\\" as a string (which contains two char). Next, because ReplaceAll is a regular expression parameter, "\ \" is interpreted as a regex. For a regex, this represents a character, which is "\". For the back of the 8, the end will be interpreted as "\ \".
In other words, suppose String.replaceall () is a normal string, not a regex, so write the code: string target = Source.replaceall (' \ \ ', ' \\\\ '); You can do it.

Forward slash (left slash), symbol is "/", backslash (right slash), symbol "\".

In Unix/linux, the path is delimited by a forward slash "/", such as "/home/hutaow", whereas in Windows, the path is separated by a backslash "\", such as "C:\Windows\System".

Sometimes we see this way of writing, "C:\\Windows\\System", that is, with two backslashes to separate the path, which is often seen in Web applications or programming, in fact, the above path can be replaced with "C:/windows/system", no error. But if it is written "C:\Windows\System", then there may be a variety of strange mistakes.

As for the reasons for the above problems, we should analyze them from the aspect of string parsing.

People who have learned programming should know that in C, when outputting a string, if you want to output a newline, then add ' \ n ' to this flag, similar to the output of a tab, plus ' \ t ', that is, the backslash ("\") This symbol will be followed by the character of the characters to escape into other characters. According to this principle, if you want to output double quotation marks (' "'), you need to enter ' \ ' so that the string containing the double quotation marks is written in memory correctly. So what if you want to enter a backslash? It's simple, just knock ' \ \ ' on it.

See here perhaps some people have seen that, if the "C:\Windows\System" this path string to the C compiler compiled, the actual memory of the string does not contain a backslash "\", even the letter immediately after the backslash is also escaped into other characters, Calling again is bound to be a problem.

String parsing is not limited to C compilers, Java compilers, parsing of some configuration files, Web servers, and so on, and will encounter the problem of parsing strings, because traditional Windows uses a single slash path-delimited form, Causes an unnecessary error to occur when parsing the file path, so there is a double backslash "\ \" to separate the path. Regardless of whether the parsing engine resolves the backslash to an escape character and eventually gets the "\" in memory, there is no problem with the result.

It can also be seen that Windows or DOS in the design of the initial consideration is not enough, in order to distinguish with some of the UNIX characteristics, the Unix forward slash "/" separated path mode into a backslash "\". One of the problems caused by this change is that in the early DOS command line, the normal file name cannot contain spaces, and if a space is included, the command parsing cannot separate it from the parameter area if the file name is entered. For example, to enter the directory "Hutaow Yuan" (which ignores the 8.3 naming rules first), enter "CD Hutaow Yuan" directly, and the command line resolves it to enter the "Hutaow" directory, and the subsequent "Yuan" is the parameter, which is obviously not expected.

In Unix, if the file name contains spaces, you can directly precede the space with a backslash "\" to escape, so good and the command parameters are distinguished from (the parameters are generally separated by a space). As in the above example, in Unix, simply enter "CD hutaow\ Yuan" (preceded by a space in front of yuan with "\"), the command line correctly identifies "Hutaow yuan" and enters this directory.

Of course, the next version of Windows has solved the whitespace problem by using other methods, such as enclosing the file name in double quotation marks.

Forward Slash, also known as the left slash, the symbol is "/";
The backslash, also called the right slash, is the symbol "\".

Often confused what is the difference between a forward slash and a backslash? Here are some summaries:

DOS Path:
C:\WINDOWS\SETTING. This is the backslash, followed by the file name.
C:\dir/p ... This is the argument that the forward slash is followed by a name order dir is the command .

In C #:

\ is an escape character in C #, as shown below

Escape sequences

Character

\’

Single quotation marks

\”

Double quotes

\\

Back slash

/

Empty

\a

Warning

\b

Backspace

\f

Page change

\ n

Line break

\ r

Add car

\ t

Horizontal tab

\v

Vertical tab

In addition, many people like to use "d:\test.txt" when getting the file path, and then the so-called escape character error occurs.
The solution is that the average person is using @ "D:\test.txt"

Windows system:

Backslash \, used in Windows system to represent the directory.
Forward slash/, URL, url address.

In asp:
".. /"represents the parent directory of the current directory;
"/" means the root directory of the website;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.