-3

I use Ruby. I want to convert string to format phone number.

Example:

  • If string have 10 character:

'0123456789' should convert to '0123-45-6789'

  • If string have 11 character:

'01234567899' should convert to '0123-456-7899'

Ravistm
  • 2,163
  • 25
  • 25
Kien Nguyen
  • 35
  • 2
  • 6
  • I would advise something like the [phonie gem](https://github.com/wmoxam/phonie#formatting). It does not only format phone numbers, it also allow to validate phone numbers. – spickermann May 27 '16 at 16:12
  • Please read "[ask]" including the links. What effort have you put into this to solve it? Did you research on the internet? If so, where and why didn't those pages help? Did you write code? If so, show us the minimal code necessary to demonstrate the problem with it, and if you didn't, why not? – the Tin Man May 27 '16 at 22:20

4 Answers4

6

Pure Ruby way, try String#insert

> "0123456789".insert(4, '-').insert(-5, '-')
#=> "0123-45-6789" 
> "01234567899".insert(4, '-').insert(-5, '-')
#=> "0123-456-7899" 

Note: The negative numbers mean that you are counting from the end of the string and positive number means that you are counting from the beginning of the string

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Gagan Gami
  • 10,121
  • 1
  • 29
  • 55
4

If you are working in a Rails project then there is a built-in view helper which can do most of the leg-work for you: 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

So, in your case:

number_to_phone('0123456789') # => '0123-45-6789'
number_to_phone('01234567899') # => '0123-456-7899'
Tom Lord
  • 27,404
  • 4
  • 50
  • 77
  • Question is about pure ruby. No rails tag. – Sergii K May 27 '16 at 13:40
  • 1
    I know. That's why I prepended my answer with "*if* you are working in Rails..." -- because there's a reasonably high chance that they are. In addition, they could potentially still include this module in their code, or copy the method source code. Or, there may be a similar helper in some other framework they're using. Why complain that I've offered an alternate solution? – Tom Lord May 27 '16 at 13:44
  • Good Lord, that's a good point! (though I would suggest "began" or "qualified" rather than "prepended")! – Cary Swoveland May 27 '16 at 20:14
3
'0123456789'.gsub(/^(\d{4})(\d+)(\d{4})$/, '\1-\2-\3')
# => "0123-45-6789" 
'01234567899'.gsub(/^(\d{4})(\d+)(\d{4})$/, '\1-\2-\3')
# => "0123-456-7899" 
Mladen Jablanović
  • 43,461
  • 10
  • 90
  • 113
1
R = /(?<=\A.{4})(.+)(?=.{4}\z)/
def break_it(str)
  str.sub(R, '-\1-')
end
break_it '0123456789'
  #=> "0123-45-6789" 
break_it '01234567899' 
  #=> "0123-456-7899" 

The regular expression can be broken down as follows.

(?<=      # begin a positive lookbehind
  \A.{4}  # match four chars at the beginning of the string
)         # end positive lookbehind
(.+)      # match one or more chars and save to capture group 1
(?=       # begin a positive lookahead
  .{4}\z  # match four chars at the end of the string 
)         # end positive lookahead
/
Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100