-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,157
  • 15
  • 45
  • 65
user2341387
  • 293
  • 1
  • 5
  • 14

7 Answers7

2

use startsWith()

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

if (toCompare.startsWith("a")) {
}
Blackbelt
  • 152,872
  • 27
  • 286
  • 297
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,071
  • 26
  • 167
  • 187
1
String arr[] = edittext.getText().toString().split(" ", 2);
String firstWord = arr[0];
stinepike
  • 52,673
  • 14
  • 90
  • 109
1

try this this will help you

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

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

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

if (s.substring(0, s.indexOf(" ")).equalsIgnoreCase("a")) {
    // do stuff
}
DemonWav
  • 53
  • 1
  • 10
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,775
  • 5
  • 43
  • 95
android
  • 71
  • 5
0

You can use substring method.

    String dummy = edittext.getText();

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

    }