红宝石 |字符串 ascii_only?方法
ascii_only?是 Ruby 中的 String 类方法,用于检查只有 ASCII字符的字符串。
Syntax: str.ascii_only?
Parameters: Here str is the string to be checked.
Returns: True for a string which has only ASCII characters otherwise return false.
示例 1:
#ruby 2.3.1
# Ruby program to demonstrate
# the ascii_only? method
# Taking a string and
# using the method
puts "95ruby2364127".ascii_only?
puts "\u{1234}".ascii_only?
输出:
true
false
示例 2:
#ruby 2.3.1
# Ruby program to demonstrate
# the ascii_only? method
# Taking a string and
# using the method
puts "STRING".ascii_only?
puts "\u{7894}".force_encoding("UTF-8").ascii_only?
输出:
true
false