Java 在 Android 上将字符串转换为整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2709253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:07:07  来源:igfitidea点击:

Converting a string to an integer on Android

javaandroidstringintegerandroid-edittext

提问by James Rattray

How do I convert a string into an integer?

如何将字符串转换为整数?

I have a textbox I have the user enter a number into:

我有一个文本框,我让用户在其中输入一个数字:

EditText et = (EditText) findViewById(R.id.entry1);
String hello = et.getText().toString();

And the value is assigned to the string hello.

并将该值分配给 string hello

I want to convert it to a integer so I can get the number they typed; it will be used later on in code.

我想把它转换成一个整数,这样我就可以得到他们输入的数字;稍后将在代码中使用它。

Is there a way to get the EditTextto a integer? That would skip the middle man. If not, string to integer will be just fine.

有没有办法得到EditText一个整数?那将跳过中间人。如果没有,字符串到整数就可以了。

采纳答案by Jon

See the Integer class and the static parseInt()method:

查看 Integer 类和静态parseInt()方法:

http://developer.android.com/reference/java/lang/Integer.html

http://developer.android.com/reference/java/lang/Integer.html

Integer.parseInt(et.getText().toString());

You will need to catch NumberFormatExceptionthough in case of problems whilst parsing, so:

如果NumberFormatException在解析时出现问题,您将需要捕获,因此:

int myNum = 0;

try {
    myNum = Integer.parseInt(et.getText().toString());
} catch(NumberFormatException nfe) {
   System.out.println("Could not parse " + nfe);
} 

回答by manuel

int in = Integer.valueOf(et.getText().toString());
//or
int in2 = new Integer(et.getText().toString());

回答by Tren Narek

You should covert String to float. It is working.

您应该将 String 转换为浮动。这是工作。

float result = 0;
 if (TextUtils.isEmpty(et.getText().toString()) {
  return;
}

result = Float.parseFloat(et.getText().toString());

tv.setText(result); 

回答by Ravi

Best way to convert your string into int is :

将字符串转换为 int 的最佳方法是:

 EditText et = (EditText) findViewById(R.id.entry1);
 String hello = et.getText().toString();
 int converted=Integer.parseInt(hello);

回答by Ashish Sahu

Use regular expression:

使用正则表达式:

int i=Integer.parseInt("hello123".replaceAll("[\D]",""));
int j=Integer.parseInt("123hello".replaceAll("[\D]",""));
int k=Integer.parseInt("1h2el3lo".replaceAll("[\D]",""));

output:

输出:

i=123;
j=123;
k=123;

回答by Ashish Sahu

Use regular expression:

使用正则表达式:

String s="your1string2contain3with4number";
int i=Integer.parseInt(s.replaceAll("[\D]", ""));

output: i=1234;

输出:i=1234;

If you need first number combination then you should try below code:

如果您需要第一个数字组合,那么您应该尝试以下代码:

String s="abc123xyz456";
int i=NumberFormat.getInstance().parse(s).intValue();

output: i=123;

输出:i=123;

回答by Abdul Rizwan

You can use the following to parse a string to an integer:

您可以使用以下内容将字符串解析为整数:

int value=Integer.parseInt(textView.getText().toString());

int value=Integer.parseInt(textView.getText().toString());

(1)input:12 then it will work.. because textview has taken this 12 number as "12" string.

(1) input:12 那么它会起作用..因为textview已经将这个12数字作为“12”字符串。

(2) input:"abdul" then it will throw an exception that is NumberFormatException. So to solve this we need to use try catch as I have mention below:

(2) input:"abdul" 那么它会抛出一个异常,即 NumberFormatException。所以为了解决这个问题,我们需要使用 try catch ,正如我在下面提到的:

  int tax_amount=20;
  EditText edit=(EditText)findViewById(R.id.editText1);
     try
       {

        int value=Integer.parseInt(edit.getText().toString());
        value=value+tax_amount;
        edit.setText(String.valueOf(value));// to convert integer to string 

       }catch(NumberFormatException ee){
       Log.e(ee.toString());
       }

You may also want to refer to the following link for more information: http://developer.android.com/reference/java/lang/Integer.html

您可能还想参考以下链接了解更多信息:http: //developer.android.com/reference/java/lang/Integer.html

回答by Brett Moan

You can also do it one line:

你也可以用一行来做:

int hello = Integer.parseInt(((Button)findViewById(R.id.button1)).getText().toString().replaceAll("[\D]", ""));

Reading from order of execution

从执行顺序读取

  1. grab the view using findViewById(R.id.button1)
  2. use ((Button)______)to cast the Viewas a Button
  3. Call .GetText()to get the text entry from Button
  4. Call .toString()to convert the Character Varying to a String
  5. Call .ReplaceAll()with "[\\D]"to replace all Non Digit Characters with "" (nothing)
  6. Call Integer.parseInt()grab and return an integer out of the Digit-only string.
  1. 使用获取视图 findViewById(R.id.button1)
  2. 用于((Button)______)将其转换ViewButton
  3. 调用 .GetText()以从 Button 获取文本条目
  4. 调用.toString()以将 Character Varying 转换为 String
  5. 调用.ReplaceAll()with"[\\D]"将所有非数字字符替换为“”(无)
  6. 调用Integer.parseInt()grab 并从仅数字字符串中返回一个整数。

回答by Ravi Makvana

Try this code it's really working.

试试这个代码它真的有效。

int number = 0;
try {
    number = Integer.parseInt(YourEditTextName.getText().toString());
} catch(NumberFormatException e) {
   System.out.println("parse value is not valid : " + e);
} 

回答by Ravi Makvana

Use regular expression is best way to doing this as already mentioned by ashish sahu

正如ashish sahu 已经提到的那样,使用正则表达式是最好的方法

public int getInt(String s){
return Integer.parseInt(s.replaceAll("[\D]", ""));
}