[英]Why does Ruby return for `str[-1..1]` what it does?
Suppose we have a string str
. 假设我们有一个字符串
str
。 If str
contains only one character, for example, str = "1"
, then str[-1..1]
returns 1
. 如果
str
只包含一个字符,例如str = "1"
,则str[-1..1]
返回1
。
But if the size
( length
) of str
is longer than one, like str = "anything else"
, then str[-1..1]
returns ""
(empty string). 但是如果
str
的size
( length
)长于1,比如str = "anything else"
str[-1..1]
str = "anything else"
,则str[-1..1]
返回""
(空字符串)。
Why does Ruby interpret string slicing like this? 为什么Ruby会像这样解释字符串切片?
This behaviour is just how ranges of characters work. 此行为只是字符范围的工作方式。
The range start is -1, which is the last character in the string. 范围start是-1,这是字符串中的最后一个字符。 The range end is 1, which is the second position from the start.
范围结束为1,这是从头开始的第二个位置。
So for a one character string, this is equivalent to 0..1, which is that single character. 因此对于一个字符串,这相当于0..1,即单个字符。
For a two character string, this is 1..1, which is the second character. 对于两个字符的字符串,这是1..1,这是第二个字符。
For a three character string, this is 2..1, which is an empty string. 对于三个字符的字符串,这是2..1,这是一个空字符串。 And so on for longer strings.
等等更长的字符串。
To get a non-trivial substring, the start position has to represent a position earlier than the end position. 要获得非平凡的子字符串,起始位置必须表示比结束位置更早的位置。
For a single-length string, index -1
is the same as index 0
, which is smaller than 1
. 对于单长度字符串,索引
-1
与索引0
相同,后者小于1
。 Thus, [-1..1]
gives a non-trivial substring. 因此,
[-1..1]
给出了一个非平凡的子串。
For a string longer than a single character, index -1
is larger than index 0
. 对于长于单个字符的字符串,索引
-1
大于索引0
。 Thus, [-1..1]
cannot give a non-trivial substring, and by default, it returns an empty string. 因此,
[-1..1]
不能给出一个非平凡的子字符串,默认情况下,它返回一个空字符串。
Writing down the indices usually helps me: 写下指数通常有助于我:
# 0 1 2 3 4 5 6 7 8 9 10 11 12
str = 'a' 'n' 'y' 't' 'h' 'i' 'n' 'g' ' ' 'e' 'l' 's' 'e' #=> "anything else"
# -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
You can refer to each character by either its positive or negative index. 您可以通过其正或负索引来引用每个字符。 For example, you can use either
3
or -10
to refer to "t"
: 例如,您可以使用
3
或-10
来表示"t"
:
str[3] #=> "t"
str[-10] #=> "t"
and either 7
or -6
to refer to "g"
: 并且
7
或-6
表示"g"
:
str[7] #=> "g"
str[-6] #=> "g"
Likewise, you can use each of these indices to retrieve "thing"
via a range: 同样,您可以使用这些索引中的每一个来通过范围检索
"thing"
:
str[3..7] #=> "thing"
str[3..-6] #=> "thing"
str[-10..7] #=> "thing"
str[-10..-6] #=> "thing"
str[-1..1]
however would return an empty string, because -1
refers to the last character and 1
refers to the second. str[-1..1]
但是会返回一个空字符串,因为-1
表示最后一个字符, 1
表示第二个字符。 It would be equivalent to str[12..1]
. 它等同于
str[12..1]
。
But if the string consists of a single character, that range becomes valid: 但如果字符串由单个字符组成,则该范围变为有效:
# 0
str = '1'
# -1
str[-1..1] #=> "1"
In fact, 1
refers to an index after the first character, so 0
would be enough: 实际上,
1
指的是第一个字符后的索引,所以0
就足够了:
str[-1..0] #=> "1"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.