如何对字符串上的每个其他字符执行操作?

时间:2021-09-27 14:28:09

How do I capitalize only a string's even letters?

如何仅将字符串的偶数字母大写?

I've tried:

str = "apples"; str.capitalize

2 个解决方案

#1


3  

"abcdefgh".gsub /..?/, &:capitalize
=> "AbCdEfGh"

"abcdefgh".gsub /(?!^)..?/, &:capitalize
=> "aBcDeFgH"

Use string.gsub! instead of string.gsub if you want to modify your original string.

使用string.gsub!如果要修改原始字符串,请使用而不是string.gsub。

#2


0  

string.scan(/..?/).map(&:capitalize) * ''

#1


3  

"abcdefgh".gsub /..?/, &:capitalize
=> "AbCdEfGh"

"abcdefgh".gsub /(?!^)..?/, &:capitalize
=> "aBcDeFgH"

Use string.gsub! instead of string.gsub if you want to modify your original string.

使用string.gsub!如果要修改原始字符串,请使用而不是string.gsub。

#2


0  

string.scan(/..?/).map(&:capitalize) * ''