🖥

#Ruby tr replace backslash and other character netime

2020/01/26に公開

Wanna replace in string

Repladce backslash with A
Repladce colon with X

puts ' \\ : '
#  \ :

# Only backslash replace
# It works
puts ' \\ : '.tr('\\', 'A')
# A :

# Replace backslash and other calacter one time
# It does not work
puts ' \\ : '.tr('\\:', 'AX')
# \ A

# Above case maybe same as this
# Only colon replacing
puts ' \\ : '.tr(':', 'AX')
# \ A

# Use expression triple backslash as backslash
# It works
puts ' \\ : '.tr('\\\:', 'AX')
# A X

# It works
# backslash at last character in tr arg
puts ' \\ : '.tr(':\\', 'XA')
# A X

Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/2960

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2020-01-26

Discussion