为什么有基于 1 的行号和基于 0 的字符号的约定?
根据 TkDocs:
这里的“1.0”代表插入文本的位置,可以读作“第1行,第0个字符”。这是指第一行的第一个字符;对于与程序员通常如何引用行和字符相关的历史约定,行号是从 1 开始的,字符号是从 0 开始的。
我以前没有听说过这个大会,在谷歌上也找不到任何相关信息。有人可以向我解释一下吗?
According to TkDocs:
The "1.0" here represents where to insert the text, and can be read as "line 1, character 0". This refers to the first character of the first line; for historical conventions related to how programmers normally refer to lines and characters, line numbers are 1-based, and character numbers are 0-based.
I hadn't heard of this convention before, and I can't find anything relevant on Google. Can anyone explain this to me please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你指的是 Tk 的文本小部件。手册页说:
不过,我不确定它指的是哪些 Unix 工具。
更新:
正如评论中提到的,看起来很多 unix 文本操作工具从 1 开始行编号。而 tcl/tk 具有 unix 起源,因此尽可能与底层操作系统环境兼容是有意义的。
I think you're referring to Tk's text widget. The man page says:
Although, I'm not sure which Unix tools it's talking about.
Update:
As mentioned in the comments, it looks like a lot of unix text manipulation tool starts line numbering at 1. And tcl/tk having a unix origin, it makes sense to be as compatible as possible with the underlying OS environment.
这确实只是惯例,但这里有一个建议。
通常认为字符位置与 Java 迭代器相同,它是指向两个字符之间位置的“指针”。因此,第一个字符是索引位置 0 之后的字符。例如,子字符串是在两个字符间位置之间获取的。
另一方面,行位置通常更多地被认为是 .NET 枚举器的方式,它是指向项目本身的“指针”,而不是指向两者之间的位置。因此第一行是位置 1 处的行。
It really is nothing more than convention, but here is a suggestion.
Character positions are generally thought of in the same way as a Java iterator, which is a "pointer" to a position between two characters. Thus the first character is the one after index position 0. Substrings are taken between two inter-character positions, for instance.
Line positions on the other hand are generally thought of more in the way of a .NET enumerator, which is a "pointer" to the item itself, not to a position in between. Thus the first line is the line at position 1.