[英]How to remove all non-digits from a string in ruby?
用户以下列形式输入数字:
1-800-432-4567
800-432-4567
800.432.4566
(800)432.4567
+1(800)-432-4567
800 432 4567
我希望所有这些都变成一个剥离版本,没有像18004324567
这样的特殊字符。 数据以String
的形式出现,因此不需要进行字符串检查。
我的方法如下:
def canonical_form number
a = remove_whitespaces number #to clear all whitespaces in between
a.gsub(/[()-+.]/,'')
end
def remove_whitespaces number
number.gsub(/\s+/,'') #removes all whitespaces
end
有一个更好的方法吗? 可以使用canonical_form
方法中的canonical_form
则表达式检查空格,而无需额外的空格方法吗? 如何以更简洁的方式对其进行重构或完成?
如果String的tr
方法的第一个参数以^
开头,那么它表示除列出的所有字符之外的所有字符。
def canonical_form str
str.tr('^0-9', '')
end
上面的几个解决方案 - 我对一些人感兴趣的基准测试:
str = "1-800-432-4567"
Benchmark.ms { 10000.times{str.scan(/\d/).join} }
#=> 69.4419999490492
Benchmark.ms { 10000.times{str.delete('^0-9')} }
#=> 7.574999995995313
Benchmark.ms { 10000.times{str.tr('^0-9', '')} }
#=> 7.642999989911914
Benchmark.ms { 10000.times{str.gsub(/\D+/, '')} }
#=> 28.084999998100102
您可以查找所有数字,而不是删除特殊字符。 就像是:
str = "1-800-432-4567"
str.scan(/\d/).join
#=> "18004324567"
str = "(800)432.4567"
str.scan(/\d/).join
#=> "8004324567"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.