Ruby 中的文本处理:回文检测与行尾转换
1. 回文检测
回文是指正读和反读都相同的字符串。最初,我们发现字符串可以反转自身,而回文字符串的定义就是它与其反转后的字符串相同。然而,简单的回文检测并不令人完全满意,它无法处理包含非字母字符的句子。因此,我们需要一个更复杂的回文检测方法。
以下是 palindrome2.rb 文件的代码:
#!/usr/bin/env ruby
# palindrome2.rb
=begin rdoc
Gives every <b>String</b> the ability to identify whether it is a
a palindrome. This version ignores all non-alphabetic characters,
making it suitable for longer text items.
=end
class String
DUAL_CASE_ALPHABET = ('a'..'z').to_a + ('A'..'Z').to_a
=begin rdoc
Contrast this with some other languages, involving iterating through each
string index and comparing with the same index from the opposite end.
Takes 1 optional Boolean, which indicates whether case matters.
Assume
超级会员免费看
订阅专栏 解锁全文
713

被折叠的 条评论
为什么被折叠?



