-2

Example: I have a EditText and 1 Button (onclick: example). I want to check the first word that typed in EditText.

public void example (View v) {
    if (edittext.getText().toString() .... //How to check the first word that typed in edittext, 
    ex: if the first word typed in edittext equalsIgnoreCase("a")){
        //Action
}     
Sam
  • 7,252
  • 16
  • 46
  • 65
user2341387
  • 303
  • 1
  • 5
  • 14

7 Answers7

2

use startsWith()

String toCompare = edittext.getText().toString();

if (toCompare.startsWith("a")) {
}
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1
if(edittext.getText().toString().split(" ")[0].equals("YOUR_STRING")) {
//DO THE TASK
}

With null/ empty check you can

if (! TextUtils.isEmpty(editText.getText())) {
            if ("LOOKING_STRING".equals(editText.getText().toString().split(" ")[0])) {
                //DO THE TASK HERE
            }
        }
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
1
String arr[] = edittext.getText().toString().split(" ", 2);
String firstWord = arr[0];
stinepike
  • 54,068
  • 14
  • 92
  • 112
1

try this this will help you

         String s=edittext.getText().toString().trim().charAt(0)+""

         if(s.equalsIgnoreCase("a")){

       give condition
           }
anddevmanu
  • 1,459
  • 14
  • 19
1
String s = edittext.getText().toString();

if (s.substring(0, s.indexOf(" ")).equalsIgnoreCase("a")) {
    // do stuff
}
DenWav
  • 53
  • 1
  • 11
1

You can also use a faster way for your condition as follows:

if (edittext.getText().toString().trim().startsWith("a")) {
      // here your code when it starts with "a"   
} else {
     // here your code when it is not
}
Blo
  • 11,903
  • 5
  • 45
  • 99
android
  • 71
  • 5
0

You can use substring method.

    String dummy = edittext.getText();

    if(dummy.substring(0,1).equalsIgnoreCase("a")){
    //your code goes here

    }