简体   繁体   中英

How to remove all non-digits from a string in ruby?

Users input numbers in the following forms:

1-800-432-4567
800-432-4567
800.432.4566
(800)432.4567
+1(800)-432-4567
800 432 4567

I want all of these to be turned into a stripped version without the special characters like 18004324567 . The data come in the form of a String , so string checking isn't required.

My method is as below:

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

Is there a better way to do this? Can the white space check with the regular expression in canonical_form method be performed without having an extra method for white spaces? How can this be refactored or done in a neater way?

If the first argument of the tr method of String starts with ^ , then it denotes all characters except those listed.

def canonical_form str
  str.tr('^0-9', '')   
end

Several solutions above - I benchmarked a few in case anyone is interested:

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

Instead of removing special characters, you can looks for all digits. Something like:

str = "1-800-432-4567"
str.scan(/\d/).join
#=> "18004324567"

str = "(800)432.4567"
str.scan(/\d/).join
#=> "8004324567"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM