将字符串电话号码转换为整数 [英] Converting the string phone number to integer

查看:83
本文介绍了将字符串电话号码转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java和Android都缺乏经验.我正在尝试检索存储在android手机模拟器的联系人之一中的电话号码.当我成功获取它时,该数字已以字符串形式返回,格式为((987)654-3210)".我想将其转换为整数或长整数.我怎样才能做到这一点?如果我使用 Integer.parseInt(String number),它将返回 NumberFormatException .尝试使用 Long.valueOf(String number)时也失败.那我该怎么办?我想要像没有任何大括号或连字符的"9876543210".

I am fairly inexperienced in Java as well as Android. I am trying to retrieve a phone number stored in one of the contacts of the android phone emulator. While I am successful to fetch it, the number has been returned in a string in a format like "(987) 654-3210". I want to convert it to integer or long. How can I do that? If I use Integer.parseInt(String number), it returns a NumberFormatException. Failed while tried using Long.valueOf(String number) too. What should I do then? I want it like "9876543210" without any braces or hyphens.

推荐答案

使用 long 存储数字会更好

   String str="(987) 654-3210";
   String stt=str.replaceAll("\\D+","");
   long num= Long.parseLong(stt);
   System.out.println(num);

这篇关于将字符串电话号码转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆