Skip to content

Commit 0090311

Browse files
[DOC] String slices doc (#14740)
1 parent 0a6cd03 commit 0090311

File tree

4 files changed

+298
-173
lines changed

4 files changed

+298
-173
lines changed

doc/string.rb

Lines changed: 0 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -159,146 +159,6 @@
159159
# - #rstrip, #rstrip!: Strip trailing whitespace.
160160
# - #strip, #strip!: Strip leading and trailing whitespace.
161161
#
162-
# == +String+ Slices
163-
#
164-
# A _slice_ of a string is a substring selected by certain criteria.
165-
#
166-
# These instance methods utilize slicing:
167-
#
168-
# - String#[] (aliased as String#slice): Returns a slice copied from +self+.
169-
# - String#[]=: Mutates +self+ with the slice replaced.
170-
# - String#slice!: Mutates +self+ with the slice removed and returns the removed slice.
171-
#
172-
# Each of the above methods takes arguments that determine the slice
173-
# to be copied or replaced.
174-
#
175-
# The arguments have several forms.
176-
# For a string +string+, the forms are:
177-
#
178-
# - <tt>string[index]</tt>
179-
# - <tt>string[start, length]</tt>
180-
# - <tt>string[range]</tt>
181-
# - <tt>string[regexp, capture = 0]</tt>
182-
# - <tt>string[substring]</tt>
183-
#
184-
# <b><tt>string[index]</tt></b>
185-
#
186-
# When a non-negative integer argument +index+ is given,
187-
# the slice is the 1-character substring found in +self+ at character offset +index+:
188-
#
189-
# 'bar'[0] # => "b"
190-
# 'bar'[2] # => "r"
191-
# 'bar'[20] # => nil
192-
# 'тест'[2] # => "с"
193-
# 'こんにちは'[4] # => "は"
194-
#
195-
# When a negative integer +index+ is given,
196-
# the slice begins at the offset given by counting backward from the end of +self+:
197-
#
198-
# 'bar'[-3] # => "b"
199-
# 'bar'[-1] # => "r"
200-
# 'bar'[-20] # => nil
201-
#
202-
# <b><tt>string[start, length]</tt></b>
203-
#
204-
# When non-negative integer arguments +start+ and +length+ are given,
205-
# the slice begins at character offset +start+, if it exists,
206-
# and continues for +length+ characters, if available:
207-
#
208-
# 'foo'[0, 2] # => "fo"
209-
# 'тест'[1, 2] # => "ес"
210-
# 'こんにちは'[2, 2] # => "にち"
211-
# # Zero length.
212-
# 'foo'[2, 0] # => ""
213-
# # Length not entirely available.
214-
# 'foo'[1, 200] # => "oo"
215-
# # Start out of range.
216-
# 'foo'[4, 2] # => nil
217-
#
218-
# Special case: if +start+ equals the length of +self+,
219-
# the slice is a new empty string:
220-
#
221-
# 'foo'[3, 2] # => ""
222-
# 'foo'[3, 200] # => ""
223-
#
224-
# When a negative +start+ and non-negative +length+ are given,
225-
# the slice begins by counting backward from the end of +self+,
226-
# and continues for +length+ characters, if available:
227-
#
228-
# 'foo'[-2, 2] # => "oo"
229-
# 'foo'[-2, 200] # => "oo"
230-
# # Start out of range.
231-
# 'foo'[-4, 2] # => nil
232-
#
233-
# When a negative +length+ is given, there is no slice:
234-
#
235-
# 'foo'[1, -1] # => nil
236-
# 'foo'[-2, -1] # => nil
237-
#
238-
# <b><tt>string[range]</tt></b>
239-
#
240-
# When a Range argument +range+ is given,
241-
# it creates a substring of +string+ using the indices in +range+.
242-
# The slice is then determined as above:
243-
#
244-
# 'foo'[0..1] # => "fo"
245-
# 'foo'[0, 2] # => "fo"
246-
#
247-
# 'foo'[2...2] # => ""
248-
# 'foo'[2, 0] # => ""
249-
#
250-
# 'foo'[1..200] # => "oo"
251-
# 'foo'[1, 200] # => "oo"
252-
#
253-
# 'foo'[4..5] # => nil
254-
# 'foo'[4, 2] # => nil
255-
#
256-
# 'foo'[-4..-3] # => nil
257-
# 'foo'[-4, 2] # => nil
258-
#
259-
# 'foo'[3..4] # => ""
260-
# 'foo'[3, 2] # => ""
261-
#
262-
# 'foo'[-2..-1] # => "oo"
263-
# 'foo'[-2, 2] # => "oo"
264-
#
265-
# 'foo'[-2..197] # => "oo"
266-
# 'foo'[-2, 200] # => "oo"
267-
#
268-
# <b><tt>string[regexp, capture = 0]</tt></b>
269-
#
270-
# When the Regexp argument +regexp+ is given,
271-
# and the +capture+ argument is <tt>0</tt>,
272-
# the slice is the first matching substring found in +self+:
273-
#
274-
# 'foo'[/o/] # => "o"
275-
# 'foo'[/x/] # => nil
276-
# s = 'hello there'
277-
# s[/[aeiou](.)\1/] # => "ell"
278-
# s[/[aeiou](.)\1/, 0] # => "ell"
279-
#
280-
# If the argument +capture+ is provided and not <tt>0</tt>,
281-
# it should be either a capture group index (integer)
282-
# or a capture group name (String or Symbol);
283-
# the slice is the specified capture
284-
# (see {Groups and Captures}[rdoc-ref:Regexp@Groups+and+Captures]):
285-
#
286-
# s = 'hello there'
287-
# s[/[aeiou](.)\1/, 1] # => "l"
288-
# s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "non_vowel"] # => "l"
289-
# s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, :vowel] # => "e"
290-
#
291-
# If an invalid capture group index is given, there is no slice.
292-
# If an invalid capture group name is given, +IndexError+ is raised.
293-
#
294-
# <b><tt>string[substring]</tt></b>
295-
#
296-
# When the single +String+ argument +substring+ is given,
297-
# it returns the substring from +self+ if found, otherwise +nil+:
298-
#
299-
# 'foo'['oo'] # => "oo"
300-
# 'foo'['xx'] # => nil
301-
#
302162
# == What's Here
303163
#
304164
# First, what's elsewhere. Class +String+:

doc/string/aref.rdoc

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
Returns the substring of +self+ specified by the arguments.
2+
3+
<b>Form <tt>self[index]</tt></b>
4+
5+
With non-negative integer argument +index+ given,
6+
returns the 1-character substring found in self at character offset index:
7+
8+
'hello'[0] # => "h"
9+
'hello'[4] # => "o"
10+
'hello'[5] # => nil
11+
'тест'[2] # => "с"
12+
'こんにちは'[4] # => "は"
13+
14+
With negative integer argument +index+ given,
15+
counts backward from the end of +self+:
16+
17+
'hello'[-1] # => "o"
18+
'hello'[-5] # => "h"
19+
'hello'[-6] # => nil
20+
21+
<b>Form <tt>self[start, length]</tt></b>
22+
23+
With integer arguments +start+ and +length+ given,
24+
returns a substring of size +length+ characters (as available)
25+
beginning at character offset specified by +start+.
26+
27+
If argument +start+ is non-negative,
28+
the offset is +start+:
29+
30+
'hello'[0, 1] # => "h"
31+
'hello'[0, 5] # => "hello"
32+
'hello'[0, 6] # => "hello"
33+
'hello'[2, 3] # => "llo"
34+
'hello'[2, 0] # => ""
35+
'hello'[2, -1] # => nil
36+
37+
If argument +start+ is negative,
38+
counts backward from the end of +self+:
39+
40+
'hello'[-1, 1] # => "o"
41+
'hello'[-5, 5] # => "hello"
42+
'hello'[-1, 0] # => ""
43+
'hello'[-6, 5] # => nil
44+
45+
Special case: if +start+ equals the length of +self+,
46+
returns a new empty string:
47+
48+
'hello'[5, 3] # => ""
49+
50+
<b>Form <tt>self[range]</tt></b>
51+
52+
With Range argument +range+ given,
53+
forms substring <tt>self[range.start, range.size]</tt>:
54+
55+
'hello'[0..2] # => "hel"
56+
'hello'[0, 3] # => "hel"
57+
58+
'hello'[0...2] # => "he"
59+
'hello'[0, 2] # => "he"
60+
61+
'hello'[0, 0] # => ""
62+
'hello'[0...0] # => ""
63+
64+
<b>Form <tt>self[regexp, capture = 0]</tt></b>
65+
66+
With Regexp argument +regexp+ given and +capture+ as zero,
67+
searches for a matching substring in +self+;
68+
updates {Regexp-related global variables}[rdoc-ref:Regexp@Global+Variables]:
69+
70+
'hello'[/ell/] # => "ell"
71+
'hello'[/l+/] # => "ll"
72+
'hello'[//] # => ""
73+
'hello'[/nosuch/] # => nil
74+
75+
With +capture+ as a positive integer +n+,
76+
returns the +n+th matched group:
77+
78+
'hello'[/(h)(e)(l+)(o)/] # => "hello"
79+
'hello'[/(h)(e)(l+)(o)/, 1] # => "h"
80+
$1 # => "h"
81+
'hello'[/(h)(e)(l+)(o)/, 2] # => "e"
82+
$2 # => "e"
83+
'hello'[/(h)(e)(l+)(o)/, 3] # => "ll"
84+
'hello'[/(h)(e)(l+)(o)/, 4] # => "o"
85+
'hello'[/(h)(e)(l+)(o)/, 5] # => nil
86+
87+
<b>Form <tt>self[substring]</tt></b>
88+
89+
With string argument +substring+ given,
90+
returns the matching substring of +self+, if found:
91+
92+
'hello'['ell'] # => "ell"
93+
'hello'[''] # => ""
94+
'hello'['nosuch'] # => nil
95+
'тест'['ес'] # => "ес"
96+
'こんにちは'['んにち'] # => "んにち"
97+
98+
Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].

0 commit comments

Comments
 (0)