[Ruby] How to use gsub method and sub method

Introduction

I will output it for Ruby learning. This time, I will introduce how to replace characters using "gsub method" and "sub method".

gsub method

You can replace all the matched parts by using the gsub method.

n =gets ← (this time tests on gets-I typed test)
m = n.gsub("test","hoge")
=> hoge-hoge(test-test is hoge-Replaced by hoge)

sub method

By using the sub method, you can replace only the first matching part.

n =gets ← (this time tests on gets-I typed test)
m = n.sub("test","hoge")
=> hoge-test(test-test is hoge-Replaced by test)

[Application] Replace all matched parts with the gsub method and replace multiple patterns

s =gets ← (Enter PAIZA in gets this time)
n = s.gsub(/A|E|G|I|O|S|Z/,"A" => 4,"E" => 3,"G"=>6,"I"=>1,"O"=>0,"S"=>5,"Z"=>2)
>> P4124

The part corresponding to the regular expression matched by the first argument is replaced with the second argument

Application summary

If you don't use regular expressions

String.gsub(置換したいString, 置換後のString)
>>Character string after replacement

When using regular expressions

String.gsub(/Regular expressions/, Regular expressionsに該当した箇所を置換した後のString)
>>Character string after replacing the part corresponding to the regular expression

Recommended Posts

[Ruby] How to use gsub method and sub method
[Ruby] How to use any? Method
How to use Ruby inject method
[Ruby on Rails] How to use session method
[Ruby basics] How to use the slice method
Ruby: How to use cookies
How to use the link_to method
How to use StringBurrer and Arrays.toString.
How to use the include? method
How to use the form_with method
[Java] How to use join method
How to use Ruby on Rails
How to use equality and equality (how to use equals)
How to use OrientJS and OrientDB together
[Ruby on Rails] How to use CarrierWave
How to use submit method (Java Silver)
[Rails] How to use the map method
[Java] How to use the toString () method
Ruby length, size, count How to use
How to set up and use kapt
[Ruby] How to use slice for beginners
[Ruby on Rails] How to use redirect_to
[Easy] How to upgrade Ruby and bundler
[Ruby on Rails] How to use kaminari
How to use substring and substr methods
Ruby: CSV :: How to use Table Note
[Rails] How to use helper method, confimartion
How to use @Builder and @NoArgsConstructor together
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
Output of how to use the slice method
How to use Segmented Control and points to note
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use the replace () method (Java Silver)
How to use scope and pass processing (Jakarta)
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
Ruby How to convert between uppercase and lowercase
How to use Map
[Java] How to use Calendar class and Date class
[Ruby] Learn how to use odd? Even? And count the even and odd numbers in the array!
[Ruby] How to calculate the total amount using the initialize method and class variables
[Java] How to use compareTo method of Date class
[Ruby] How to use standard output in conditional branching