将各种符号的字符串转换为Integer

时间:2014-04-24 17:11:39

标签: java string numbers integer

是否可以将此+37068941556之类的字符串转换为整数或长整数或任何数字格式(在这种情况下,我想将其转换为数字37068941556。提前致谢!

尝试使用parseLong()时收到的错误的堆栈跟踪:

04-24 20:18:46.253: E/AndroidRuntime(31258): FATAL EXCEPTION: main
04-24 20:18:46.253: E/AndroidRuntime(31258): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nrsearch/com.example.nrsearch.MainActivity}: java.lang.NumberFormatException: Invalid long: "+37068941556"
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.app.ActivityThread.access$700(ActivityThread.java:159)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.os.Looper.loop(Looper.java:176)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.app.ActivityThread.main(ActivityThread.java:5419)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at java.lang.reflect.Method.invokeNative(Native Method)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at java.lang.reflect.Method.invoke(Method.java:525)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at dalvik.system.NativeStart.main(Native Method)
04-24 20:18:46.253: E/AndroidRuntime(31258): Caused by: java.lang.NumberFormatException: Invalid long: "+37068941556"
04-24 20:18:46.253: E/AndroidRuntime(31258):    at java.lang.Long.invalidLong(Long.java:125)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at java.lang.Long.parse(Long.java:362)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at java.lang.Long.parseLong(Long.java:353)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at java.lang.Long.parseLong(Long.java:319)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at com.example.nrsearch.MainActivity$PlaceholderFragment.onCreateView(MainActivity.java:94)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1188)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.app.Activity.performStart(Activity.java:5382)
04-24 20:18:46.253: E/AndroidRuntime(31258):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
04-24 20:18:46.253: E/AndroidRuntime(31258):    ... 11 more

1 个答案:

答案 0 :(得分:1)

试试这个:

String text = "+37068941556";
System.out.println(text.replaceAll("\\+", ""));