skip to Main Content

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


  1. .trim() would remove whitespaces from the input, but you should probably then use two inputs, one for first name and one for last name

    val string = "Bobby  "
    val trimmed = string.trim()
    

    Trimmed = "Bobby"

    Login or Signup to reply.
  2. 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:

    Sting input = "input ";
    while (input.charAt(input.length()-1) == ' '){
            input = input.substring(0, input.length()-1);
        }
    
    Login or Signup to reply.
  3. 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:

    1. Get the UI element, in which the user will type:

    EditText firstName = requireActivity().findViewById(R.id.registerFirstName);

    1. Take the string value of the user’s input and validate it as you wish:

    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.

    Login or Signup to reply.
  4. Use string.trim()

    why to use trim()

    The trim() method in Java String is a built-in function that
    eliminates leading and trailing spaces. The Unicode value of space
    character is ‘u0020’. The trim() method in java checks this Unicode
    value before and after the string, if it exists then removes the
    spaces and returns the omitted string.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search