[Ruby] Insert, replace, destroy at the end of the character string [b021]

Method memorandum and first post that I googled when checking a certain skill

1. Get the output value Get any number of input characters as an array during loop processing


hoge  #Characters to be entered
huga  #Characters to be entered
piga  #Characters to be entered

lines = []  #Define array variables in advance
lines << gets.chomp  #Chomp so there are no line breaks

-Since the character string comes over multiple lines, add it to the array


lines = ["hoge", "fuga", "piga"]
puts lines[0]  #Output any character by specifying the index number (subscript)
>> hoge

-Output any character with the index number after making necessary changes according to the calling conditions.

2. Insert at the end of the character Consider a conditional branch to insert or replace depending on the type at the end of the string.


#Conditional branch
String.end_with?('Value you want to find')  #任意の文字末尾にValue you want to findがあるか
puts hoge.end_with?('e', 'ge', 'ch')  #Multiple values can be referenced at once (any one is acceptable)
>> ture

#Insert method
String.insert(Insertion index number,"Value to insert") 
#['h','o','g','e']The index number of is from the beginning[0,1,2,3], From the end[-4,-3,-2,-1]
puts hoge.insert(-1,"s")  #Insert at the end
>> hoges

puts hoge.insert(0,"s")  #Insert at the beginning
>> shoge

#Destructive method
String.delete_suffix("Value to erase")  #末尾のValue to eraseを消去
puts gehoge.delete_suffix("ge")
>> geho  #The beginning does not disappear

#Replace (end)"ge"To"ver"Convert to)
puts gehoge.delete_suffix("ge").insert(-1,"ver")
>> hehover

reference https://qiita.com/uuchan/items/a4e9382440bdc4d2ac75 https://qiita.com/prgseek/items/92b49fe6b0a579f9cdd8

I wish I could replace it quickly, but it was difficult, so I pushed it with a matching technique If you know a better way, please comment!

Recommended Posts

[Ruby] Insert, replace, destroy at the end of the character string [b021]
[Delete the first letter of the character string] Ruby
[Ruby] Specify the argument name of the method. Meaning of the colon (:) at the end
[Android] Add an arbitrary character string at the beginning of multiple lines
Count the number of occurrences of a string in Ruby
The nth and n + 1st characters of a Ruby string
Why put a line break at the end of the file
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
[Rails] How to omit the display of the character string of the link_to method
About the behavior of ruby Hash # ==
Replace the contents of the Jar file
[Ruby] See the essence of ArgumentError
Various methods of the String class
I read the source of String
[Ruby] Display the contents of variables
[Ruby] I want to output only the odd-numbered characters in the character string
Memorandum No. 3 "Replace the entered character string for each character depending on the conditions"
[Java] Check if the character string is composed only of blanks (= Blank)