将字符串转换为 phone 数字 Ruby
Convert string to phone number Ruby
我用Ruby。
我想将字符串转换为格式 phone number.
示例:
- 如果字符串有 10 个字符:
'0123456789' should convert to '0123-45-6789'
- 如果字符串有 11 个字符:
'01234567899' should convert to '0123-456-7899'
'0123456789'.gsub(/^(\d{4})(\d+)(\d{4})$/, '--')
# => "0123-45-6789"
'01234567899'.gsub(/^(\d{4})(\d+)(\d{4})$/, '--')
# => "0123-456-7899"
如果您在 Rails 项目中工作,那么有一个内置的视图助手可以为您完成大部分的腿部工作:http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_to_phone
number_to_phone(5551234) # => 555-1234
number_to_phone("5551234") # => 555-1234
number_to_phone(1235551234) # => 123-555-1234
number_to_phone(1235551234, area_code: true) # => (123) 555-1234
number_to_phone(1235551234, delimiter: " ") # => 123 555 1234
number_to_phone(1235551234, area_code: true, extension: 555) # => (123) 555-1234 x 555
number_to_phone(1235551234, country_code: 1) # => +1-123-555-1234
number_to_phone("123a456") # => 123a456
number_to_phone("1234a567", raise: true) # => InvalidNumberError
因此,在您的情况下:
number_to_phone('0123456789') # => '0123-45-6789'
number_to_phone('01234567899') # => '0123-456-7899'
纯粹的Ruby方式,试试String#insert
> "0123456789".insert(4, '-').insert(-5, '-')
#=> "0123-45-6789"
> "01234567899".insert(4, '-').insert(-5, '-')
#=> "0123-456-7899"
注意:负数表示从字符串末尾算起,正数表示从字符串开头算起[=13=]
R = /
(?<=\A\d{4}) # match four digits at beginning of string in positive lookbehind
| # or
(?=\d{4}\z) # match four digits at end of string in positive lookahead
/x # free-spacing regex definition mode
def break_it(str)
str.split(R).join('-')
end
break_it '0123456789'
#=> "0123-45-6789"
break_it '01234567899'
#=> "0123-456-7899"
我会推荐这个吗?不,我更喜欢@Gagan 的解决方案,如果没有采用,我会提供它。我提出这个解决方案是为了引起普遍兴趣。
我用Ruby。 我想将字符串转换为格式 phone number.
示例:
- 如果字符串有 10 个字符:
'0123456789' should convert to '0123-45-6789'
- 如果字符串有 11 个字符:
'01234567899' should convert to '0123-456-7899'
'0123456789'.gsub(/^(\d{4})(\d+)(\d{4})$/, '--')
# => "0123-45-6789"
'01234567899'.gsub(/^(\d{4})(\d+)(\d{4})$/, '--')
# => "0123-456-7899"
如果您在 Rails 项目中工作,那么有一个内置的视图助手可以为您完成大部分的腿部工作:http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_to_phone
number_to_phone(5551234) # => 555-1234
number_to_phone("5551234") # => 555-1234
number_to_phone(1235551234) # => 123-555-1234
number_to_phone(1235551234, area_code: true) # => (123) 555-1234
number_to_phone(1235551234, delimiter: " ") # => 123 555 1234
number_to_phone(1235551234, area_code: true, extension: 555) # => (123) 555-1234 x 555
number_to_phone(1235551234, country_code: 1) # => +1-123-555-1234
number_to_phone("123a456") # => 123a456
number_to_phone("1234a567", raise: true) # => InvalidNumberError
因此,在您的情况下:
number_to_phone('0123456789') # => '0123-45-6789'
number_to_phone('01234567899') # => '0123-456-7899'
纯粹的Ruby方式,试试String#insert
> "0123456789".insert(4, '-').insert(-5, '-')
#=> "0123-45-6789"
> "01234567899".insert(4, '-').insert(-5, '-')
#=> "0123-456-7899"
注意:负数表示从字符串末尾算起,正数表示从字符串开头算起[=13=]
R = /
(?<=\A\d{4}) # match four digits at beginning of string in positive lookbehind
| # or
(?=\d{4}\z) # match four digits at end of string in positive lookahead
/x # free-spacing regex definition mode
def break_it(str)
str.split(R).join('-')
end
break_it '0123456789'
#=> "0123-45-6789"
break_it '01234567899'
#=> "0123-456-7899"
我会推荐这个吗?不,我更喜欢@Gagan 的解决方案,如果没有采用,我会提供它。我提出这个解决方案是为了引起普遍兴趣。