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

using another class in a camelprocessor
Register page with Spring Security
Running JAR file in C# without providing path to the PROCESS
JAVA: apply width and height to fragment
Initialize a hashmap in a more compact way without external libraries
Setting Image Anchor properties of image using jxl
JPA OneToOne Lazy relation
How to find path trough blocks in a grid
Android BitmapFactory often returning null on Base64 decoded byte array
Can I force spring-data to update auditable fields even if JPA entity not dirty?
JavaFX: Change font and size in a TextField
form:input path giving exception
Sending HTTP request on Android over non TCP transport
Java , how can i split this sentence into array?
Maven how to automate installation of dependencies?
Count occurrences of an approximate date in an entity

Categories

HOME
wso2-am
oracle11g
gremlin
nullpointerexception
include
youtube-dl
jira
dxl
qore
awesome-wm
kentor-authservices
google-cloud-ml
spring-tool-suite
orchardcms
quickfix
try-catch
clojurescript
ghc
progressive-web-apps
invantive-sql
openedx
swiftlint
xlsxwriter
mps
cultureinfo
javacv
nhapi
minitab
ejabberd-module
libuv
react-chartjs
.net-4.0
semantic-versioning
ioio
noraui
commit
x11-forwarding
withings
streamsets
midl
bootstrap-dialog
ionicons
integrity
idisposable
brightcove
arrow-keys
django-scheduler
jquery-filter
wdf
android-fonts
libpng
vmware-tools
android-textview
measures
thrust
google-web-starter-kit
lttng
pagerank
r-tree
reactive-banana
computer-algebra-systems
mono-embedding
composite
notify
device-orientation
nsmutabledictionary
id3v2
socketexception
prettify
page-layout
apc
google-cloud-save
flash-builder4.5
gwt-rpc
android-screen-support
angularjs-controller
manchester-syntax
online-compilation
enter
gnu-prolog
stage
psi
invite
rfc1123
javax.script
telerik-scheduler
fluent-interface
suppress
modelstate
thunderbird-lightning
webkit.net
ti-dsp
w3wp

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App