Description
I am developing one app in which I have registration page. Inside registration page, I am doing registration by getting user’s full name and mobile number.
Problem
While getting user’s full name in edit text sometimes the user is pressing space bar after typing his/her name.
I don’t need space in my database after typing any text white user.
4
Answers
.trim()
would remove whitespaces from the input, but you should probably then use two inputs, one for first name and one for last nameTrimmed = "Bobby"
not quite sure how your structure works, but if I understand the question correctly you just need a while loop going backwards from the end of the string checking to see if it is a space, if it is, remove it.
This assumes your input contains both first and last name, and only removes the whitespace at the end.
something like this should work:
I would also recommend that you implement some kind of validations. For example, for an only whitespaces(spaces, tabs, new lines, etc…) input.
An example:
EditText firstName = requireActivity().findViewById(R.id.registerFirstName);
String firstNameStr = firstName.getText().toString();
if (firstNameStr.trim().equals("")) { Make a Toast or something }
This way you are certain there are no extra whitespaces and the input is not empty.
Use
string.trim()
why to use trim()