Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove blank spaces from email and password get by EditText

Tags:

java

android

EditText recEmail=(EditText)findViewbyId(R.id.email);
EditText recPassword=(EditText)findViewbyId(R.id.password);
String email=recEmail.getText().toString();
String password=recPassword.getText().toString();

The email and password contains the white spaces at start,in or end of string if tab is pressed by mistake.

I need text out of white spaces, please help if you can.

Thanks in advance....!!!

like image 748
aalionline Avatar asked Nov 23 '11 15:11

aalionline


2 Answers

String.trim() is your friend.

String email=recEmail.getText().toString().trim();
String password=recPassword.getText().toString().trim();

In the future, I highly recommend checking the Java String methods in the API. It's a lifeline to getting the most out of your Java environment.

As a general rule, I would not get rid of whitespace in the text. What if the user's password starts or ends with a space? They will never be able to log in. I personally think you're better off just leaving your code as is.

like image 111
Jason Nichols Avatar answered Sep 23 '22 05:09

Jason Nichols


This is a technically very advanced operation, but can be achieved by extreme coding precision:

mahString = mahString.trim();

like image 39
pgsandstrom Avatar answered Sep 22 '22 05:09

pgsandstrom