java


Convert phone number “+2125551212” to format “(212) 555-1212” using Twilio SDK


I have a phone number stored as a string String in the format +12125551212. Using the Twilio SDK (if possible) I would like to convert that phone number into a String in the more friendly format of (212) 555-1212.
Here's how one could do it using com.google.i18n.phonenumbers
String phoneNumber = "+12125551212";
try {
Phonenumber.PhoneNumber parsedPhoneNumber = phoneUtil.parse(phoneNumber, "US");
phoneNumber = phoneUtil.format(parsedPhoneNumber, PhoneNumberUtil.PhoneNumberFormat.NATIONAL);
} catch (NumberParseException e) {
logger.error("Error parsing phone number {}", phoneNumber);
}
Is there something similar in the Twilio SDK?
(I'm hoping for a non-regex, non-string-manipulation Twilio-specific answer, if it exists.)
Twilio developer evangelist here.
You can do that and much more using Twilio's Lookup API. You will find the API documentation here.
Here's a full working example of how you would format a telephone number using the API.
import java.util.HashMap;
import java.util.Map;
import com.twilio.Twilio;
import com.twilio.rest.lookups.v1.PhoneNumber;
public class Lookup {
// Find your Account Sid and Token at twilio.com/user/account
public static final String ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
public static final String AUTH_TOKEN = "your_auth_token";
public static void main(String[] args) {
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
PhoneNumber number = PhoneNumber
.fetcher(new com.twilio.type.PhoneNumber("+15108675309"))
.setType("carrier")
.fetch();
System.out.println(number.getNationalFormat());
}
}
Hope this helps you!
https://twilio.github.io/twilio-java/
Go to PhoneNumber, it has a method called Get National Format and also look at PhoneNumber.Type that might be what you're looking for.
Otherwise I would just use string indexing + parsing, and create a utility function that converts nationalToUS or USToNational etc.

Related Links

how readUTF() method of DataInputStream finish form Keyboard?
How can I initialize interdependent final references?
Beginner: Min Value in Array (Java)
Matrix Multiplication using different classes - Java
What's wrong in my JAVA code? [closed]
Java Selection for calendaring leap year
Spring,Hibernate with mysql image is not displaying properly
Group similar items in a JTable
Re-run script upon obtaining error on a webpage
reference to println is ambiguous error
DEBUG: Using PENTAHO_JAVA_HOME
How do I copy element which reference from int array >=40 to a new String array
My Programming Lab standard output errors
Injecting List in Map using Spring 2.5
PHP and Java, how to join them?
How to parse a string which holds date of format “Oct 1, 2015 12:00:00 AM”?

Categories

HOME
gerrit
webpack
reflection
image-processing
pycharm
layout
fft
tesseract
relay
rsyslog
cmd
amazon-ecs
rubygems
v8
networkx
azure-media-services
portia
gnupg
numeral.js
correlation
remote-access
foselasticabundle
samoa
extjs5
phpfox
entitlements
filezilla
greendao
fluentvalidation
large-file-upload
webkitspeechrecognition
librato
create-table
webtest
framemaker
asset-pipeline
repo
mozilla
y86
appfabric
serverside-rendering
objectlistview
webdriver-manager
avro4s
import-from-excel
modelmapper
s
slick-3.0
websphere-mq-fte
multipeer-connectivity
boost-multi-index
python-webbrowser
crypt
nbconvert
minimization
root-framework
firmata
nsfilemanager
django-debug-toolbar
contact-list
simplewebrtc
moveit
php-parse-error
tmuxinator
muse
tableau-online
php-ci
ios9.1
sniffer
p2
clipperlib
android-nested-fragment
fpml
aapt
jquery-layout
poller
cloud-connect
typo3-neos
openlaszlo
floating-point-precision
ccss
xsockets.net
dbconnection
enterprisedb
clipper
e4x
parametric-equations
bigcouch
external-accessory
subgurim-maps
genshi
xdomainrequest
winbugs14
gdlib
castle-monorail
sef
sustainable-pace
noscript
private-members
lzh

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App