0
1

More than 1 year has passed since last update.

#Ruby の tr でバックスラッシュと他の文字が同時に置換できない?

Last updated at Posted at 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

チャットメンバー募集

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

Twitter

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1