简体   繁体   English

什么反斜杠“\”真的意味着什么?

[英]What does back slash “\” really mean?

I'm wondering about Java's backslash. 我想知道Java的反斜杠。 How does the computer or the compiler see this backslash and how is it stored in computer? 计算机或编译器如何看到这个反斜杠以及它是如何存储在计算机中的?

I read that backslash removes the special meaning of the following character. 我读到反斜杠删除了以下字符的特殊含义。 But how does a computer treat this one and in what conditions treat it in some other ways? 但是,计算机如何处理这个以及在什么条件下以其他方式对待它?

For example the null character \\0 in C programming, is the end of the string, but is it a single character or two characters, ie, backslash + zero? 例如,C编程中的空字符\\0是字符串的结尾,但它是单个字符还是两个字符,即反斜杠+零?

The objective of back slash is to indicate for humans or to indicate for 0-1 computer? 反斜杠的目的是指示人类还是指示0-1计算机?

The backslash \\ is a character, just like the letter A , the comma , , and the number 4 . 反斜杠\\是一个性格,就像信A ,逗号,和数字4 In some programming languages, notably C and its descendants (and maybe ancestors), it is used inside a string or character literal to escape other characters. 在某些编程语言中,特别是C及其后代(可能是祖先),它在字符串或字符文字中用于转义其他字符。 For instance, '\\a' represents the bell character, and will produce a beep from the computer if you print it ( printf("%c", '\\a') ). 例如, '\\a'代表铃声字符,如果你打印它会从计算机发出一声蜂鸣声( printf("%c", '\\a') )。

As a C-language escape character, it is largely a human construct allowed by the compiler so humans can express, eg, the bell character. 作为C语言转义字符,它主要是编译器允许的人类构造,因此人类可以表达例如钟形字符。 The compiled code simply stores the character — a byte with the value 7. Just to be absolutely clear, it does not store a \\ followed by an a . 编译后的代码只存储字符-与值7字节只是要非常清楚,它存储\\后跟a

Under other contexts, the backslash means something to the program at runtime. 在其他情况下,反斜杠在运行时对程序意味着什么。 The most well-known instance of this is regular expression syntax, in which a backslash escape other characters in order to either give them special meaning or take away a special meaning they might have. 最着名的实例是正则表达式语法,其中反斜杠转义其他字符以便赋予它们特殊含义或带走它们可能具有的特殊含义。 For example, grep '\\<foo\\>' file.txt will locate lines with the word foo in file.txt. 例如, grep '\\<foo\\>' file.txt将在file.txt中找到带有单词foo的行。 In this case the backslashes really are there at runtime, and are interpreted by the program as escapes for the < and > that follow them. 在这种情况下,反斜杠确实在运行时存在,并由程序解释为跟随它们的<>转义。 In this case, \\< and \\> don't represent characters at all; 在这种情况下, \\<\\>根本不代表字符; they denote a zero-width match against the beginning and end of a word, respectively. 它们分别表示与单词的开头和结尾的零宽度匹配。

It really depends entirely on the context. 完全取决于具体情况。 Backslashes can mean many different things, depending on where you see them used. 反斜杠可能意味着许多不同的东西,具体取决于您看到它们的使用位置。 For example, in Windows, backslashes are commonly found as path separators. 例如,在Windows中,通常将反斜杠视为路径分隔符。

In C-based programming languages (as well as other scripting languages), they are escape characters . 在基于C语言的编程语言(以及其他脚本语言)中,它们是转义字符 They indicate that the character to their right should be interpreted differently from the way it normally would. 他们指出,他们右边的角色应该与通常的角色不同。 In your example, \\0 is a null-terminator, and the backslash indicates that the 0 should be interpreted by the compiler (and the human!) as the null character instead of as the number zero. 在您的示例中, \\0是一个空终止符,反斜杠表示编译器(和人类!)应该将0解释为空字符而不是数字零。 It would be seen as just one character because the backslash is dropped off once its function is served—it has no meaning in that sequence beyond its use as an escape character. 它将被视为只有一个字符,因为一旦其函数被提供,反斜杠就会被删除 - 除了用作转义字符之外,它在该序列中没有任何意义。

A backslash only means something special if the situation it is used in deems it to be. 反斜杠只表示如果使用的情况认为是特殊的东西。 For example, many programming languages use them as escape characters in strings. 例如,许多编程语言将它们用作字符串中的转义字符 This usually changes the meaning of the next character. 这通常会改变下一个字符的含义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM