作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一串文字:
string = "%hello %world ho%w is i%t goin%g"
我要返回以下内容:
"Hello World hoW is iT goinG
%
符号是告诉我下一个字符应该大写的键。到目前为止我得到的最接近的是:
@thing = "%this is a %test this is %only a %test"
if @thing.include?('%')
indicator_position = @thing.index("%")
lowercase_letter_position = indicator_position + 1
lowercase_letter = @thing[lowercase_letter_position]
@thing.gsub!("%#{lowercase_letter}","#{lowercase_letter.upcase}")
end
返回:
"This is a Test this is %only a Test"
看起来我需要遍历字符串才能使其正常工作,因为它只是替换了小写字母“t”,但我无法使其正常工作。
最佳答案
你可以用 gsub
和一个 block 来做到这一点:
string.gsub(/%(.)/) do |m|
m[1].upcase
end
使用 block 允许您在每次匹配时运行任意代码。
关于ruby - 如何替换字符串中的特定字符以及紧邻的下一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37420332/
我正在运行 lynx 来测试一些我必须做的 HTML,因为考虑到了可访问性。我猜想,如果 Lynx 看起来很漂亮,那么所有屏幕阅读器、蹩脚手机和其他东西都可以使用,甚至是最古老的硬件。 在某些情况下,
我想,这是一个非常简单的问题,但我就是想不通。 数组包含整数列表,如果数组中的每个数字“x”后跟数字“y”,我想返回 true。 因此数组具有 {x,3,4,y} 或 {x,x,y,4,5} 或 {5
我是一名优秀的程序员,十分优秀!