Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要将第一个字符放在方括号内。例如,我想转换"abc"为"[a]bc". 如何使用正则表达式在 ruby 中执行此操作?
"abc"
"[a]bc"
使用正则表达式:
"abc".sub(/(.)/, '[\1]')
没有正则表达式:
s = "abc" s[0] = "[#{s[0]}]"
尝试这个 :
s = "abc" p "abc".insert(1, ']').prepend('[') #=> "[a]bc"