--Character string.sub (/ converted character /, "character string you want to replace) ・ ・ ・ Convert only the first character
irb(main):026:0> str
=> "123123123"
irb(main):027:0> str.sub(/12/,"ab")
=> "ab3123123"
irb(main):028:0> str
=> "123123123"
When I converted the letter 12 to ab, it was replaced with ab3123123 However, the instance itself has not been replaced
--Character string.gsub (/ converted character /, "character string you want to replace) ・ ・ ・ Convert all corresponding characters g means global
irb(main):029:0> str.gsub(/12/,"ab")
=> "ab3ab3ab3"
Again, the instance itself is not replaced
--String.sub! (/ Converted character /, "Character string you want to replace) ... Only the first character of the original instance itself is changed
!! Is a destructive method ... transforms the instance itself
irb(main):030:0> str.sub!(/12/,"ab")
=> "ab3123123"
irb(main):031:0> str
=> "ab3123123"
--String.gsub! (/ Converted character /, "Character string you want to replace) ... Changes all characters in the original variable itself
irb(main):034:0> str = "123123123"
=> "123123123"
irb(main):035:0> str.gsub!(/12/,"ab")
=> "ab3ab3ab3"
irb(main):036:0> str
=> "ab3ab3ab3"
--Character string.index ("Character string you want to search") ・ ・ ・ Search and tell you where the character string you want to search is.
irb(main):038:0> "12345".index("2")
=> 1
Of course, you can also search for strings stored in variables.
irb(main):041:0> str = "abcde"
=> "abcde"
irb(main):042:0> str.index("c")
=> 2
Looking at this, you thought that "1" was not strange even though you were searching for "2" with "12345", right? Stored elements are assigned numbers such as subscripts Since the subscript starts from 0, the character string "12345" is assigned from "0" instead of being assigned from "1". That's why the search result for "2" became "1".
--Character string.delete ("Character string you want to delete") ・ ・ ・ Delete the specified character string --Character string.delete! ("Character string you want to delete") ... (also from the instance) Delete the specified character string
irb(main):044:0> str.delete("cd")
=> "abe"
irb(main):045:0> str
=> "abcde"
Of course! If you add, it will be a destructive method, so the instance will also be changed (deleted).
--Character string.chop ・ ・ ・ You can delete the end of the line of the character string.
irb(main):049:0> str.chop
=> "abcd"
This too! Is a destructive method
--Character string.chomp ・ ・ ・ Line feed code can also be deleted
For example, if you enter a character string using gets without chomo
irb(main):054:0> gets.to_s
My name is Taro
=> "My name is Taro\n"
And \ n and line feed code are built in
If you use chomp it
irb(main):053:0> gets.to_s.chomp
My name is Taro
=> "My name is Taro"
\ n is not included
String .split
irb(main):055:0> "aa bb cc".split
=> ["aa", "bb", "cc"]
If you insert a space, the array will be split and created. As an aside, when creating a tag function with rails, if there is no gem, use split to create multiple (experience story) It is also possible to specify a character and divide it, for example, if ","
You can split by specifying a comma with the string .split (",")
Example:
irb(main):058:0> "aa,bb,cc".split(",")
=> ["aa", "bb", "cc"]
How to return the created array to a character string Use the array .join
Example: Put it in what["aa", "bb", "cc"]Is stored
irb(main):059:0> what.join
=> "aabbcc"
If you specify the array .join ("")
irb(main):061:0> what.join(" ")
=> "aa bb cc"
Return to the string in the split state
Character string .upcase ・ ・ ・ Convert to uppercase Character string .downcase ・ ・ ・ Convert to lowercase
irb(main):063:0> length = "aaa"
=> "aaa"
irb(main):064:0> length.upcase
=> "AAA"
irb(main):065:0> length.upcase!
=> "AAA"
irb(main):066:0> length
=> "AAA"
irb(main):067:0> length.downcase
=> "aaa"
irb(main):068:0> length.downcase!
=> "aaa"
irb(main):069:0> length
=> "aaa"
Character string .reverse ・ ・ ・ Reverses the character string horizontally
irb(main):072:0> length = "abc"
=> "abc"
irb(main):073:0> length.reverse
=> "cba"
Character string.slice (range) ・ ・ ・ Cuts the specified range of the character string
irb(main):076:0> length = "abcdef"
=> "abcdef"
irb(main):077:0> length.slice(2..4)
=> "cde"
Cut out the string cde in the range 2-4 This slice (2..4) .. is used when doing a range
I tried to summarize what was in the notebook from one end Maybe the interpretation is wrong, so please point it out if you like.
Recommended Posts