Home > front end >  How to get into a variable only a part of the entered in EditText?
How to get into a variable only a part of the entered in EditText?

Time:01-25

How to get into a variable in Java only part of the text or numbers entered in EditText (android). For example, from the entered number 123456789 to get only the first three digits 123?

Looked on the Internet but didnt find anything sensible

CodePudding user response:

You can use the substring method to get the number of characters you want:

String num = editText.toString().substring(0,3); //<-- gets the first 3 
                                                    characters of input

https://www.javatpoint.com/java-string-substring

CodePudding user response:

You can use this:

    EditText editText;
    String slice;
    editText = findViewById(R.id.edit_text);
    if (editText.getText().toString().length() >= 2){
        slice = editText.getText().toString().substring(0,2);
    } else {
        slice = editText.getText().toString();
        Log.d("TAG", "onCreate: got only possible letters");
    }
    Log.d("TAG", "slice = "   slice);
  • Related