String Comparison - Android

Comparing strings in Java

[EDIT]
I made a mistake earlier, because, to get the text, you need to use .getText().toString().

Here is a full working example:

package com.psegina.passwordTest01;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Main extends Activity implements OnClickListener {
LinearLayout l;
EditText user;
EditText pwd;
Button btn;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

l = new LinearLayout(this);
user = new EditText(this);
pwd = new EditText(this);
btn = new Button(this);

l.addView(user);
l.addView(pwd);
l.addView(btn);
btn.setOnClickListener(this);

setContentView(l);
}

public void onClick(View v){
String u = user.getText().toString();
String p = pwd.getText().toString();
if( u.equals( p ) )
Toast.makeText(getApplicationContext(), "Matches", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), user.getText()+" != "+pwd.getText(), Toast.LENGTH_SHORT).show();
}
}

Original answer (Will not work because of the lack of toString())

Try using .getText() instead of .toString().

if( passw1.getText() == passw2.getText() )
#do something

.toString() returns a String representation of the whole object, meaning it won't return the text you entered in the field (see for yourself by adding a Toast which will show the output of .toString())

Android string compare is false

Use equals() to compare strings for content, not ==.

== will check if the objects are the same.

String foo = "foo";

if (foo == foo) {
// same object, so true
}
String foo1 = "foo";
String foo2 = "foo";

if (foo1 == foo2) {
// both are string literals set at compile time, so true
}
String foo1 = loadFoo1(); // imagine this returns "foo"
String foo2 = loadFoo2(); // imagine this also returns "foo"

if (foo1 == foo2) {
// not the same object, and not string literals, so false
}

if (foo1.equals(foo2)) {
// both strings hold "foo", so true
}

Comparing two string in Java not giving the expected result

You have to use String.equals() method for string matching.

Change your code into this

    if (editText.getText().toString().equals("admin")) {
//your code
}

.

String comparison not working in android

public void processFinish(String output) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), output,Toast.LENGTH_LONG).show();

String check="false";
if(output.trim().equals(check.trim())){
//doing something here
}else{
//something here
}
}

your string getting some unwanted blank space from method trim() remove starting and end blank space :) and .equals is a object class method which compare two string :)

Android Studio Java String Compare

It seems that String s has un-necessary characters present.
Try below:

if(s.contains("success"){}

Note: For string comparison, its always good practice to compare constant value with other string. This will save code from Null pointers.

if("success".equalsIgnoreCase(s)){}

Compare multiple strings android

If you have have a variable number of strings to check I would do something like the following:

private boolean validate(String... items) {
for (String item : items) {
if (TextUtils.isEmpty(item)) {
return false;
}
}
return true;
}

Then your code would call the validate method with as many strings as you need to validate:

if (validate(string1, string2, stringN)) {
Toast.makeText(this, "something is empty", Toast.LENGTH_SHORT).show();
} else {
// GO TO THE NEXT ACTIVITY
}

How to Compare two strings in android with optimization?

According to me, You should use the equals() method of the String class to compare Strings.

if (xyz.equals("something")) { 
//Do what you want if string is equal...
}

see this also..

comparing two strings of EditTexts in android

Try it like this:

if(x.equalsIgnoreCase(y))
{
//----stuff--;
}

Follow this link to know more about it :
http://www.leepoint.net/data/expressions/22compareobjects.html

if statement comparing strings in java android

In java, when using == on two objects, you're not actually comparing the strings themselves. You'll need to use .equals(String).

== actually compares the two object's references, not their values.

string1.equals(String target) compares the two strings based off of the actual characters in the strings.

See: http://www.leepoint.net/notes-java/data/expressions/22compareobjects.html



Related Topics



Leave a reply



Submit