mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00

* Enhanced RDoc for String#split * Enhanced RDoc for String#split * Enhanced RDoc for String#split * Enhanced RDoc for String#split * Enhanced RDoc for String#split
142 lines
5.2 KiB
Text
142 lines
5.2 KiB
Text
# :markup: ruby
|
||
|
||
class String
|
||
# call-seq:
|
||
# String.new(string = '') -> new_string
|
||
# String.new(string = '', encoding: encoding) -> new_string
|
||
# String.new(string = '', capacity: size) -> new_string
|
||
#
|
||
# Returns a new \String that is a copy of +string+.
|
||
#
|
||
# With no arguments, returns the empty string with the Encoding <tt>ASCII-8BIT</tt>:
|
||
# s = String.new
|
||
# s # => ""
|
||
# s.encoding # => #<Encoding:ASCII-8BIT>
|
||
#
|
||
# With the single \String argument +string+, returns a copy of +string+
|
||
# with the same encoding as +string+:
|
||
# s = String.new('Que veut dire ça?')
|
||
# s # => "Que veut dire ça?"
|
||
# s.encoding # => #<Encoding:UTF-8>
|
||
#
|
||
# Literal strings like <tt>""</tt> or here-documents always use
|
||
# Encoding@Script+encoding, unlike String.new.
|
||
#
|
||
# With keyword +encoding+, returns a copy of +str+
|
||
# with the specified encoding:
|
||
# s = String.new(encoding: 'ASCII')
|
||
# s.encoding # => #<Encoding:US-ASCII>
|
||
# s = String.new('foo', encoding: 'ASCII')
|
||
# s.encoding # => #<Encoding:US-ASCII>
|
||
#
|
||
# Note that these are equivalent:
|
||
# s0 = String.new('foo', encoding: 'ASCII')
|
||
# s1 = 'foo'.force_encoding('ASCII')
|
||
# s0.encoding == s1.encoding # => true
|
||
#
|
||
# With keyword +capacity+, returns a copy of +str+;
|
||
# the given +capacity+ may set the size of the internal buffer,
|
||
# which may affect performance:
|
||
# String.new(capacity: 1) # => ""
|
||
# String.new(capacity: 4096) # => ""
|
||
#
|
||
# The +string+, +encoding+, and +capacity+ arguments may all be used together:
|
||
#
|
||
# String.new('hello', encoding: 'UTF-8', capacity: 25)
|
||
#
|
||
def initialize(str = '', encoding: nil, capacity: nil)
|
||
Primitive.rb_str_init(str, encoding, capacity)
|
||
end
|
||
|
||
# call-seq:
|
||
# split(separator = $;, limit = nil) -> array
|
||
# split(separator = $;, limit = nil) {|substring| ... } -> self
|
||
#
|
||
# Returns an array of substrings of +self+
|
||
# that are the result of splitting +self+
|
||
# at each occurrence of the given +separator+.
|
||
#
|
||
# When argument +separator+ is <tt>$;</tt>:
|
||
#
|
||
# - If <tt>$;</tt> is +nil+ (its default value),
|
||
# the split occurs just as if +separator+ were given as a space character
|
||
# (see below).
|
||
#
|
||
# - If <tt>$;</tt> is a string,
|
||
# the split ocurs just as if +separator+ were given as that string
|
||
# (see below).
|
||
#
|
||
# When argument +separator+ is <tt>' '</tt> and argument +limit+ is +nil+,
|
||
# the split occurs at each sequence of whitespace:
|
||
#
|
||
# 'abc def ghi'.split(' ') # => ["abc", "def", "ghi"]
|
||
# "abc \n\tdef\t\n ghi".split(' ') # => ["abc", "def", "ghi"]
|
||
# 'abc def ghi'.split(' ') # => ["abc", "def", "ghi"]
|
||
# ''.split(' ') # => []
|
||
#
|
||
# When argument +separator+ is a string different from <tt>' '</tt>
|
||
# and argument +limit+ is +nil+,
|
||
# the split occurs at each occurrence of the separator;
|
||
# trailing empty substrings are not returned:
|
||
#
|
||
# 'abracadabra'.split('ab') # => ["", "racad", "ra"]
|
||
# 'aaabcdaaa'.split('a') # => ["", "", "", "bcd"]
|
||
# ''.split('a') # => []
|
||
# '3.14159'.split('1') # => ["3.", "4", "59"]
|
||
# '!@#$%^$&*($)_+'.split('$') # => ["!@#", "%^", "&*(", ")_+"]
|
||
# 'тест'.split('т') # => ["", "ес"]
|
||
# 'こんにちは'.split('に') # => ["こん", "ちは"]
|
||
#
|
||
# When argument +separator+ is a Regexp and argument +limit+ is +nil+,
|
||
# the split occurs at each occurrence of a match;
|
||
# trailing empty substrings are not returned:
|
||
#
|
||
# 'abracadabra'.split(/ab/) # => ["", "racad", "ra"]
|
||
# 'aaabcdaaa'.split(/a/) # => ["", "", "", "bcd"]
|
||
# 'aaabcdaaa'.split(//) # => ["a", "a", "a", "b", "c", "d", "a", "a", "a"]
|
||
# '1 + 1 == 2'.split(/\W+/) # => ["1", "1", "2"]
|
||
#
|
||
# If the \Regexp contains groups, their matches are also included
|
||
# in the returned array:
|
||
#
|
||
# '1:2:3'.split(/(:)()()/, 2) # => ["1", ":", "", "", "2:3"]
|
||
#
|
||
# As seen above, if argument +limit+ is +nil+,
|
||
# trailing empty substrings are not returned;
|
||
# the same is true if +limit+ is zero:
|
||
#
|
||
# 'aaabcdaaa'.split('a') # => ["", "", "", "bcd"]
|
||
# 'aaabcdaaa'.split('a', 0) # => ["", "", "", "bcd"]
|
||
#
|
||
# If +limit+ is positive integer +n+, no more than <tt>n - 1-</tt>
|
||
# splits occur, so that at most +n+ substrings are returned,
|
||
# and trailing empty substrings are included:
|
||
#
|
||
# 'aaabcdaaa'.split('a', 1) # => ["aaabcdaaa"]
|
||
# 'aaabcdaaa'.split('a', 2) # => ["", "aabcdaaa"]
|
||
# 'aaabcdaaa'.split('a', 5) # => ["", "", "", "bcd", "aa"]
|
||
# 'aaabcdaaa'.split('a', 7) # => ["", "", "", "bcd", "", "", ""]
|
||
# 'aaabcdaaa'.split('a', 8) # => ["", "", "", "bcd", "", "", ""]
|
||
#
|
||
# Note that if +separator+ is a \Regexp containing groups,
|
||
# their matches are in the returned array, but do not count toward the limit.
|
||
#
|
||
# If +limit+ is negative, it behaves the same as if +limit+ was +nil+,
|
||
# meaning that there is no limit,
|
||
# and trailing empty substrings are included:
|
||
#
|
||
# 'aaabcdaaa'.split('a', -1) # => ["", "", "", "bcd", "", "", ""]
|
||
#
|
||
# If a block is given, it is called with each substring:
|
||
#
|
||
# 'abc def ghi'.split(' ') {|substring| p substring }
|
||
#
|
||
# Output:
|
||
#
|
||
# "abc"
|
||
# "def"
|
||
# "ghi"
|
||
#
|
||
def split; end
|
||
|
||
end
|