Ruby/Справочник/String: различия между версиями

Содержимое удалено Содержимое добавлено
Строка 715:
<source lang=ruby>str.split(pattern=$;, [limit]) #-> anArray</source>
----
Делит строку ''str'' на подстроки по разделителю ''pattern'' (который может быть как [[Ruby/Справочник/Regexp|правилом]], так и [[Ruby/Справочник/String|строкой]]). Если разделитель ''pattern'' не указан, то деление происходит по пробельному символу (если иное не присвоено специальной переменной <tt>$;</tt>). В результате деления возвращается [[Ruby/Справочник/Array|массив]], который содержит фрагменты строки ''str'' (сам разделитель в результат не входит).
Divides <i>str</i> into substrings based on a delimiter, returning an array of these substrings.
 
If <i>pattern</i> is a <tt>String</tt>, then its contents are used as the delimiter when splitting <i>str</i>. If <i>pattern</i> is a single space, <i>str</i> is split on whitespace, with leading whitespace and runs of contiguous whitespace characters ignored.
Если разделитель ''pattern'' является правилом, то деление производится по подстрокам, подходящим под данное правило. Если ''pattern'' — строка, то деление производится по подстрокам, которые совпадают с разделителем.
If <i>pattern</i> is a <tt>Regexp</tt>, <i>str</i> is divided where the pattern matches. Whenever the pattern matches a zero-length string, <i>str</i> is split into individual characters.
 
If <i>pattern</i> is omitted, the value of <tt>$;</tt> is used. If <tt>$;</tt> is <tt>nil</tt> (which is the default), <i>str</i> is split on whitespace as if ` ' were specified.
Если задан необязательный параметр ''limit'', то результирующий массив будет иметь количество фрагментов строки ''str'' равное ''limit''. Последний элемент будет содержать остаток, который, возможно, еще можно поделить (то есть в строке есть еще разделители).
If the <i>limit</i> parameter is omitted, trailing null fields are suppressed. If <i>limit</i> is a positive number, at most that number of fields will be returned (if <i>limit</i> is <tt>1</tt>, the entire string is returned as the only entry in an array). If negative, there is no limit to the number of fields returned, and trailing null fields are not suppressed.
<source lang=ruby>" now's the time".split #-> ["now's", "the", "time"]
" now's the time".split(' ') #-> ["now's", "the", "time"]
Строка 732:
"1,2,,3,4,,".split(',', 4) #-> ["1", "2", "", "3,4,,"]
"1,2,,3,4,,".split(',', -4) #-> ["1", "2", "", "3", "4", "", ""]</source>
 
===String#squeeze===
----