如何在 Java 中将电话号码格式化为字符串?
我一直将电话号码存储为很长的长度,我想在将电话号码打印为字符串时简单地添加连字符。
我尝试使用 DecimalFormat
但它不喜欢连字符。可能是因为它用于格式化十进制数字而不是长整数。
long phoneFmt = 123456789L;
DecimalFormat phoneFmt = new DecimalFormat("###-###-####");
System.out.println(phoneFmt.format(phoneNum)); //doesn't work as I had hoped
理想情况下,我也想在区号上加上括号。
new DecimalFormat("(###)-###-####");
这样做的正确方法是什么?
I have been storing phone numbers as longs and I would like to simply add hyphens when printing the phone number as a string.
I tried using DecimalFormat
but that doesn't like the hyphen. Probably because it is meant for formatting decimal numbers and not longs.
long phoneFmt = 123456789L;
DecimalFormat phoneFmt = new DecimalFormat("###-###-####");
System.out.println(phoneFmt.format(phoneNum)); //doesn't work as I had hoped
Ideally, I would like to have parenthesis on the area code too.
new DecimalFormat("(###)-###-####");
What is the correct way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
您可以使用 String.replaceFirst 使用正则表达式方法,例如
You can use String.replaceFirst with regex method like
要获得所需的输出:
控制台上的结果如下所示:
对于存储电话号码,您应该考虑 使用数字以外的数据类型。
To get your desired output:
The result at the Console looks like this:
For storing phone numbers, you should consider using a data type other than numbers.
最简单的方法是使用 javax 中内置的 MaskFormatter .swing.文本库。
你可以这样做:
The easiest way to do this is by using the built in MaskFormatter in the javax.swing.text library.
You can do something like this :
如果您确实需要正确的方法,那么您可以使用 Google 最近开源的 libphonenumber
If you really need the right way then you can use Google's recently open sourced libphonenumber
您还可以使用 https://github.com/googlei18n/libphonenumber。这是一个示例:
您可以在此处获取类路径上的库:http://mvnrepository。 com/artifact/com.googlecode.libphonenumber/libphonenumber
You could also use https://github.com/googlei18n/libphonenumber. Here is an example:
Here you can get the library on your classpath: http://mvnrepository.com/artifact/com.googlecode.libphonenumber/libphonenumber
最糟糕的解决方案可能是:
The worst possible solution would be:
这就是我最终这样做的方式:
我知道这不支持国际号码,但我没有编写“真正的”应用程序,所以我不担心这一点。我只接受 10 个字符长的电话号码。我只是想用一些格式打印它。
感谢您的回复。
This is how I ended up doing it:
I understand that this does not support international numbers, but I'm not writing a "real" application so I'm not concerned about that. I only accept a 10 character long as a phone number. I just wanted to print it with some formatting.
Thanks for the responses.
您可以实现自己的方法来为您做到这一点,我建议您使用类似的方法。使用
DecimalFormat
和MessageFormat
。使用此方法,您几乎可以使用任何您想要的内容(String、Integer、Float、Double
),并且输出将始终正确。结果将是
You can implement your own method to do that for you, I recommend you to use something such as this. Using
DecimalFormat
andMessageFormat
. With this method you can use pretty much whatever you want (String,Integer,Float,Double
) and the output will be always right.Result will be
DecimalFormat
不允许对数字内的任意文本进行格式化,就像前缀或后缀一样。所以它无法帮助你。在我看来,将电话号码存储为数值是完全错误的。如果我想存储国际号码怎么办?许多国家/地区使用
+
表示国家/地区代码(例如,+1
表示美国/加拿大),其他国家/地区则使用00
(例如001< /代码>)。
这两者都不能真正用数字数据类型表示(“这个数字是 1555123 还是 001555123?”)
DecimalFormat
doesn't allow arbitrary text within the number to be formatted, just as a prefix or a suffix. So it won't be able to help you there.In my opinion, storing a phone number as a numeric value is wrong, entirely. What if I want to store an international number? Many countries use
+
to indicate a country code (e.g.+1
for USA/Canda), others use00
(e.g.001
).Both of those can't really be represented in a numeric data type ("Is that number 1555123 or 001555123?")
您也可以使用子字符串和连接来轻松格式化。
但需要注意的一件事是,您必须检查电话号码字段的长度,以确保格式安全。
You could use the substring and concatenation for easy formatting too.
But one thing to note is that you must check for the lenght of the telephone number field just to make sure that your formatting is safe.
您可以将任何包含非数字字符的字符串格式化为您想要的格式,使用我的 util 类来格式化
用法非常简单的
输出:(123)-456-7890
您可以指定任何格式,例如 XXX-XXX-XXXX 和手机长度number ,如果输入长度大于指定长度,则字符串将被修剪。
从这里获取我的课程: https://github.com/gajeralalji/PhoneNumberUtil/blob /master/PhoneNumberUtil.java
U can format any string containing non numeric characters also to your desired format use my util class to format
usage is very simple
output: (123)-456-7890
u can specify any foramt such as XXX-XXX-XXXX and length of the phone number , if input length is greater than specified length then string will be trimmed.
Get my class from here: https://github.com/gajeralalji/PhoneNumberUtil/blob/master/PhoneNumberUtil.java
我认为您需要使用 MessageFormat 而不是 DecimalFormat。这样应该更灵活。
I'd have thought you need to use a MessageFormat rather than DecimalFormat. That should be more flexible.
String formatterPhone = String.format("%s-%s-%s",phoneNumber.substring(0, 3),phoneNumber.substring(3, 6),phoneNumber.substring(6, 10));
String formatterPhone = String.format("%s-%s-%s", phoneNumber.substring(0, 3), phoneNumber.substring(3, 6), phoneNumber.substring(6, 10));
使用 StringBuilder 来提高性能。
Using
StringBuilder
for performance.Kotlin
结果将是
0888-9999-8888
Kotlin
The result will be
0888-9999-8888
我用了这个
String columValue = "1234567890
String number = columValue.replaceFirst("(\d{3})(\d{3})(\d+)", "($1) $2-$3");
I used this one
String columValue = "1234567890
String number = columValue.replaceFirst("(\d{3})(\d{3})(\d+)", "($1) $2-$3");
我编写了以下代码
这是一些示例用法的测试代码:
I wrote the following code
This is some test code that exemplifies the usage: