[Ruby] When you want to replace multiple characters

gsub method

If you want to replace multiple characters, you can use gsub to replace them as specified. I received a comment from @scivola and corrected the article. @scivola Thank you very much for your kindness!

gsub [Replace all the parts you want to replace]

The following example

x = "TOKYO"
y = x.gsub(/[TOKY]/, "T" => "5", "O" => "3", "K" => "6", "Y" => "1")
puts y
=> 53613

It will replace the corresponding ones character by character. Also, since it is converted to a character string at the time of replacement, the result does not change whether you write 5 or "5".

String # tr method

Also, for "replacement" that replaces one character with one character, it seems simpler and faster to use the dedicated method String # tr than to use gsub!

y = x.tr("TOKY", "5361")

Recommended Posts

[Ruby] When you want to replace multiple characters
When you want to dynamically replace Annotation in Java8
When you want to bind InputStream in JDBI3
When you want to use the method outside
I want to convert characters ...
If you want to know the options when configuring Ruby, see `RbConfig :: CONFIG ["configure_args "]`
How to replace characters that you do not understand [Principle]
When you want to explicitly write OR or AND with ransack
When you want to change the MySQL password of docker-compose
docker-compose.yml when you want to keep mysql running with docker
lombok.config when you want to pass @Qualifier to @RequiredArgsConstructor with lombok
Delegate is convenient to use when you want to reuse parts
[Ruby + Rails] When you want to register in Mailchimp's mail list together with user registration
[Swift] How to replace multiple strings
ProxyFactory is convenient when you want to test AOP in Spring!
[Docker] Magic command when you want to wipe out none image
Practice to use when you want to execute different processing groups serially
[Swift] When you want to know if the number of characters in a String matches a certain number ...
[Ruby] I want to output only the odd-numbered characters in the character string
When you want Rails to disable a session for a specific controller only
Summary of means when you want to communicate with HTTP on Android
Basic Rails commands you want to learn
If you want to make a zip file with Ruby, it's rubyzip.
When you want to notify an error somewhere when using graphql-spring-boot in Spring Boot
Android Studio memo that you want to display Toast characters in large size
I want to sort by tab delimited by ruby
If you want to modify database columns etc.
I want to use arrow notation in Ruby
[Ruby] I want to do a method jump!
[Ruby] When adding a null constraint to a table
I want to get the value in Ruby
[Ruby] When you want to assign the result obtained by conditional branching to a variable and put it in the argument
Introduction to Ruby 2
[rails] After option useful when you want to change the order of DB columns
VScode extension to learn efficiently when you are just starting programming with ruby, rails
Command when you want to erase all Docker images and containers cleanly and start over
Code to use when you want to process Json with only standard library in Java
Method summary to update multiple columns [Ruby on Rails]
If you want to separate Spring Boot + Thymeleaf processing
How to add characters to display when using link_to method
How to think when you suddenly understand about generics
An introductory book to read when you start Rails
If you want to use Mockito with Kotlin, use mockito-kotlin
If you want to recreate the instance in cloud9
A memo to check when you try to use Lombok
In Java, I want to trim multiple specified characters from only the beginning and end.
How to write in Model class when you want to save binary data in DB with PlayFramework
When you want to change the wording to be displayed when making a select box from enum
Use JLine when you want to handle keystrokes on the console character by character in Java
When you want to implement Java library testing in Spock with multi-module in Gradle in Android Studio 3
When you want to reflect the Master Branch information in the Current Branch you are currently working on