什么反斜杠“ \”表示实际意思? [英] What does back slash "\" really mean?

查看:489
本文介绍了什么反斜杠“ \”表示实际意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道Java的反斜线。计算机或编译器如何看到此反斜杠以及如何将其存储在计算机中?



我读到反斜杠删除了以下字符的特殊含义。但是计算机如何处理这种情况,以及在什么条件下以其他方式处理它?<​​/ p>

例如空字符 \0 在C编程中是字符串的结尾,但是它是一个字符还是两个字符,即反斜杠+零?



反斜杠的目的是指示人类还是指示0-1台计算机?

解决方案

反斜杠 \ 是一个字符,就像字母 A 一样,逗号和数字 4 。在某些编程语言中,尤其是C及其子代(也许是祖代),在字符串或字符文字内部使用它来转义其他字符。例如,'\a'代表铃铛字符,并且在打印时会从计算机发出蜂鸣声( printf(%c ,'\a'))。



作为C语言转义字符,在很大程度上是编译器允许的人为构造因此人类可以表达例如钟声。编译后的代码只存储字符-一个值为7的字节。为完全清楚起见,它 not 不会存储 \ a



在其他上下文中,反斜杠在运行时对程序意味着某些意义。最著名的例子是正则表达式语法,其中反斜杠转义其他字符,以赋予它们特殊的含义或取消它们可能具有的特殊含义。例如, grep‘\< foo\>’file.txt 将在file.txt中找到带有单词foo的行。在这种情况下,反斜杠确实在运行时出现,并且由程序解释为< > 跟着他们。在这种情况下, \< \> 根本不代表字符;它们分别表示一个单词的开头和结尾的零宽度匹配。


I'm wondering about Java's backslash. 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, i.e., backslash + zero?

The objective of back slash is to indicate for humans or to indicate for 0-1 computer?

解决方案

The backslash \ is a character, just like the letter A, the comma ,, and the number 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. For instance, '\a' represents the bell character, and will produce a beep from the computer if you print it (printf("%c", '\a')).

As a C-language escape character, it is largely a human construct allowed by the compiler so humans can express, e.g., the bell character. 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.

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. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆