如何使用Ruby 1.9检测字符串是否仅包含拉丁符号? [英] How to detect if string contains only latin symbols using Ruby 1.9?
本文介绍了如何使用Ruby 1.9检测字符串是否仅包含拉丁符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要检测某个字符串是否包含非拉丁字母的符号.数字和特殊符号(例如-
,_
,+
)很好.我需要知道是否有任何非拉丁符号.例如:
I need to detect if some string contains symbols from a non latin alphabet. Numbers and special symbols like -
, _
, +
are good. I need to know whether there is any non latin symbols. For example:
"123sdjjsf-4KSD".just_latin?
应返回true
.
"12333ыц4--sdf".just_latin?
应返回false
.
推荐答案
我认为这应该对您有用:
I think that this should work for you:
# encoding: UTF-8
class String
def just_latin?
!!self.match(/^[a-zA-Z0-9_\-+ ]*$/)
end
end
puts "123sdjjsf-4KSD".just_latin?
puts "12333ыц4--sdf".just_latin?
请注意,*#ascii_only?*也非常接近您想要的.
Note that *#ascii_only?* is very close to what you want as well.
这篇关于如何使用Ruby 1.9检测字符串是否仅包含拉丁符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文