Any better way to reformat a phone number string?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Any better way to reformat a phone number string?

Rick Flower
Hi all.. I've got a string handling/reformatting question for you.. I've
got a string I get from a web-app that is in the format shown below:

'(213) 234-5678'

I'd like to modify the string so it can be inserted as a numeric value
into a database so I need to ditch all formatting such as spaces,
parenthesis,etc..  So, the resulting string should look like :
2132345678 which can be inserted into my database as an integer or fixed
character array..

Please go easy on me -- this is trivial for me in other languages but
I'm just not sure the way I went about this is efficient or not.. Below
is the code I've got that does work, but I am guessing there are cleaner
ways to go..  The input string appears to be in a ByteString object if
that helps..

    inPhone  := '(213) 234-5678'
    outPhone := String new: 10.
    idx := 1.
    inPhone  do: [:each | (each isDigit) ifTrue: [
          outPhone at: idx put: each.
          idx := idx + 1
       ]
    ]

One thing I didn't look for was to see if there's some way to tell
Smalltalk that I've got a format it can follow and do some sort of
direct translation into a numeric value -- kinda the opposite of
NumberPrintPolicy...

TIA!

Reply | Threaded
Open this post in threaded view
|

Re: Any better way to reformat a phone number string?

Rick Flower
Rick Flower wrote:
> Hi all.. I've got a string handling/reformatting question for you.. I've
> got a string I get from a web-app that is in the format shown below:
>
> '(213) 234-5678'
  [...]

Nevermind.. In further poking around in the base visualworks classes I
found code that allows me to do the following.. Exactly what I was
looking for.. Never mind!

    inPhone  := '(213) 234-5678'.
    outPhone := inPhone asNumberFromFormatString: nil

Reply | Threaded
Open this post in threaded view
|

Re: Any better way to reformat a phone number string?

Randy Coulman
In reply to this post by Rick Flower

On 4/18/07, Rick Flower <[hidden email]> wrote:

    inPhone  := '(213) 234-5678'
    outPhone := String new: 10.
    idx := 1.
    inPhone  do: [:each | (each isDigit) ifTrue: [
          outPhone at: idx put: each.
          idx := idx + 1
       ]
    ]


How about:

outPhone := inPhone select: [:each | each isDigit]

Randy
--
Randy Coulman
[hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Any better way to reformat a phone number string?

Travis Griggs-3
In reply to this post by Rick Flower

On Apr 18, 2007, at 12:33, Rick Flower wrote:

Hi all.. I've got a string handling/reformatting question for you.. I've got a string I get from a web-app that is in the format shown below:

'(213) 234-5678'

I'd like to modify the string so it can be inserted as a numeric value into a database so I need to ditch all formatting such as spaces, parenthesis,etc..  So, the resulting string should look like : 2132345678 which can be inserted into my database as an integer or fixed character array..

 ('(213) 234-5678' select: [:char | char isDigit]) asNumber

Or load SymbolValue from the OpenRepository, and you can do it as:

 ('(213) 234-5678' select: #isDigit) asNumber

--
Travis Griggs
Objologist
"Dying men never wish they'd spent more time at the office"


Reply | Threaded
Open this post in threaded view
|

Re: Any better way to reformat a phone number string?

Travis Griggs-3
In reply to this post by Rick Flower
On Apr 18, 2007, at 12:33, Rick Flower wrote:

Hi all.. I've got a string handling/reformatting question for you.. I've got a string I get from a web-app that is in the format shown below:

'(213) 234-5678'

I'd like to modify the string so it can be inserted as a numeric value into a database so I need to ditch all formatting such as spaces, parenthesis,etc..  So, the resulting string should look like : 2132345678 which can be inserted into my database as an integer or fixed character array..

If you want "efficient" for the number form..

'(213) 234-5678' inject: 0
into: [:accum :char |
char isDigit
ifTrue: [char asInteger - 48 + (accum * 10)]
ifFalse: [accum]]

is almost 2x faster than the select:/asNumber variants (the SymbolValue is as fast as the block version btw)

I would use the above only after you'd convinced yourself that that process was the single critical point of performance. Seeing as how the words "web-app" are involved... I can't see how that could be. :)

--
Travis Griggs
Objologist
"I choose. Therefore, I Am"


Reply | Threaded
Open this post in threaded view
|

Re: Any better way to reformat a phone number string?

Rick Flower
Travis Griggs wrote:

> If you want "efficient" for the number form..
>
> '(213) 234-5678' inject: 0
> into: [:accum :char |
> char isDigit
> ifTrue: [char asInteger - 48 + (accum * 10)]
> ifFalse: [accum]]
>
> is almost 2x faster than the select:/asNumber variants (the SymbolValue
> is as fast as the block version btw)
>
> I would use the above only after you'd convinced yourself that that
> process was the single critical point of performance. Seeing as how the
> words "web-app" are involved... I can't see how that could be. :)

Thanks Travis.. I'll keep that in mind if I find a lag in doing these
sort of operations, but I really don't believe there'll be any problem
with the short/slower methods since people aren't going to be updating
phone numbers constantly (at least I hope not) in their profiles.. (8->

Reply | Threaded
Open this post in threaded view
|

Re: Any better way to reformat a phone number string?

Alexander Lazarevic'
In reply to this post by Travis Griggs-3
Travis Griggs schrieb:
> '(213) 234-5678' inject: 0
> into: [:accum :char |
> char isDigit
> ifTrue: [char asInteger - 48 + (accum * 10)]
> ifFalse: [accum]]

Getting funky you could also leave out the inject:into: and do

n := 0.
'(213) 234-5678'
   do: [:char |
      char isDigit ifTrue: [
         n := (n bitShift: 1) + (n bitShift: 3) + (char asInteger - 48)]].

where n contains the number afterwards. On a single run this is about
2.4 times faster. In a tight loop it is about 1.2 times slower. ;)

Alex