What is the difference between tr and gsub?

后端 未结 3 1752
没有蜡笔的小新
没有蜡笔的小新 2020-12-12 18:24

I was reading the Ruby documentation and got confused with the difference between gsub and tr. What is the difference between the two?

3条回答
  •  时光说笑
    2020-12-12 19:15

    • tr can only replace a single character with a single fixed character (although you can put multiple matches of this sort in a single tr call) but is fast.
    • gsub can match complicated patterns using regex, and replace with a complicated computation result, but is slower than tr.

提交回复
热议问题