![]() When negative integer index is given, the slice begins at the offset given by counting backward from the end of self: 'bar' # => "b" 'bar' # => "r" 'bar' # => nil When non-negative integer argument index is given, the slice is the 1-character substring found in self at character offset index: 'bar' # => "b" 'bar' # => "r" 'bar' # => nil 'тест' # => "с" 'こんにちは' # => "は" String#slice! returns self with a slice removed.Įach of the above methods takes arguments that determine the slice to be copied or replaced. String#= returns a copy of self with a slice replaced. String# (also aliased as String#slice) returns a slice copied from self. These instance methods make use of slicing: Strip, strip!: strip leading and trailing whitespace.Ī slice of a string is a substring that is selected by certain criteria. Rstrip, rstrip!: strip trailing whitespace. Lstrip, lstrip!: strip leading whitespace. Whitespace is relevant for these methods: In class String, whitespace is defined as a contiguous sequence of characters consisting of any mixture of the following: ![]() Special match variables such as $1, $2, $`, $&, and $' are set appropriately. If argument replacement is a hash, and pattern matches one of its keys, the replacing string is the value for that key: h = # => "ABCD" You may want to use the block form to avoid a lot of backslashes. If you want to write a non-back-reference string \& in replacement, you need first to escape the backslash to prevent this method from interpreting it as a back-reference, and then you need to escape the backslashes again to prevent a string literal from consuming them: ".\\\\&.". For example, if you want to write a back-reference \& in replacement with a double-quoted string literal, you need to write ".\\&.". See String Literals for details about string literals.Ī back-reference is typically preceded by an additional backslash. Note also that a string literal consumes backslashes. Note that \\ is interpreted as an escape, i.e., a single backslash. ![]() + corresponds to $+, which contains last capture group. \` corresponds to $`, which contains string before match. \' corresponds to $', which contains string after match. \& and \0 correspond to $&, which contains the complete matched text. However, you may refer to some special match variables using these combinations: Note that within the string replacement, a character combination such as $& is treated as ordinary text, and not as a special match variable. \n ( n a non-negative integer) refers to $n. String replacement may contain back-references to the pattern’s captures: If replacement is a string, that string will determine the replacing string that is to be substituted for the matched text.Įach of the examples above uses a simple string as the replacing string. When pattern is a string, all its characters are treated as ordinary characters (not as regexp special characters): 'THX1138'. The examples in this section mostly use methods String#sub and String#gsub the principles illustrated apply to all four substitution methods.Īrgument pattern is commonly a regular expression: s = 'hello' s. String#gsub!: Zero or more substitutions returns self.Ī first argument, pattern (string or regexp), that specifies the substring(s) to be replaced.Ī second argument, replacement (string or hash), that determines the replacing string.Ī block that will determine the replacing string. String#gsub: Zero or more substitutions returns a new string. String#sub!: One substitution (or none) returns self. String#sub: One substitution (or none) returns a new string. ![]() However, a method without a bang can also mutate, such as String#replace. In general, if there exist both bang and non-bang version of method, the bang! mutates and the non-bang! does not. Typically, a method whose name ends with ! modifies self and returns self often a similarly named method (without the !) returns a new string. You can convert certain objects to Strings with: You can create a String object explicitly with: String objects differ from Symbol objects in that Symbol objects are designed to be used as identifiers, instead of text or data. A String object may be created using String::new or as literals. A String object has an arbitrary sequence of bytes, typically representing text or binary data.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |