![]() 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 |