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 ,
65returns 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
2933from 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] .
0 commit comments