Skip to content

Commit e0b2e2f

Browse files
BurdetteLamarpeterzhu2118
authored andcommitted
[DOC] Tweaks for String#index
1 parent 9b40837 commit e0b2e2f

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

doc/string/index.rdoc

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
1-
Returns the integer index of the first match for the given argument,
2-
or +nil+ if none found;
3-
the search of +self+ is forward, and begins at position +offset+ (in characters).
1+
Returns the integer position of the first substring that matches the given argument +pattern+,
2+
or +nil+ if none found.
43

5-
With string argument +substring+,
4+
When +pattern+ is a string,
65
returns the index of the first matching substring in +self+:
76

87
'foo'.index('f') # => 0
98
'foo'.index('o') # => 1
109
'foo'.index('oo') # => 1
1110
'foo'.index('ooo') # => nil
12-
'тест'.index('с') # => 2
13-
'こんにちは'.index('ち') # => 3
11+
'тест'.index('с') # => 2 # Characters, not bytes.
12+
'こんにちは'.index('ち') # => 3
1413

15-
With Regexp argument +regexp+, returns the index of the first match in +self+:
14+
When +pattern is a Regexp, returns the index of the first match in +self+:
1615

1716
'foo'.index(/o./) # => 1
1817
'foo'.index(/.o/) # => 0
1918

20-
With positive integer +offset+, begins the search at position +offset+:
19+
When +offset+ is non-negative, begins the search at position +offset+;
20+
the returned index is relative to the beginning of +self+:
2121

22-
'foo'.index('o', 1) # => 1
23-
'foo'.index('o', 2) # => 2
24-
'foo'.index('o', 3) # => nil
22+
'bar'.index('r', 0) # => 2
23+
'bar'.index('r', 1) # => 2
24+
'bar'.index('r', 2) # => 2
25+
'bar'.index('r', 3) # => nil
26+
'bar'.index(/[r-z]/, 0) # => 2
2527
'тест'.index('с', 1) # => 2
26-
'こんにちは'.index('ち', 2) # => 3
28+
'тест'.index('с', 2) # => 2
29+
'тест'.index('с', 3) # => nil # Offset in characters, not bytes.
30+
'こんにちは'.index('ち', 2) # => 3
2731

28-
With negative integer +offset+, selects the search position by counting backward
32+
With negative integer argument +offset+, selects the search position by counting backward
2933
from the end of +self+:
3034

3135
'foo'.index('o', -1) # => 2
@@ -35,4 +39,4 @@ from the end of +self+:
3539
'foo'.index(/o./, -2) # => 1
3640
'foo'.index(/.o/, -2) # => 1
3741

38-
Related: String#rindex.
42+
Related: see {Querying}[rdoc-ref:String@Querying].

string.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4501,8 +4501,7 @@ rb_strseq_index(VALUE str, VALUE sub, long offset, int in_byte)
45014501

45024502
/*
45034503
* call-seq:
4504-
* index(substring, offset = 0) -> integer or nil
4505-
* index(regexp, offset = 0) -> integer or nil
4504+
* index(pattern, offset = 0) -> integer or nil
45064505
*
45074506
* :include: doc/string/index.rdoc
45084507
*

0 commit comments

Comments
 (0)