Search code examples
androidstring-formattingeditview

How to ignore spaces between text editview Android


I am trying to ignore spaces in editview between text, I am not quite sure how I can go about doing this. I know I can use trim feature to ignore spaces before and after the full text but how do I ignore space between strings if there is any;

String myTextEdited myText.getText().toString().trim();

For example, if I have / user types in this;

Allan Bob
3523 JKO
NY1 U90

I want to ingore spaces when I read this in my if statement or put it in another variable for example;

String name = "AllanBob"

For example, to ignore upper and lower cases I am doing this;

 if (myText.getText().toString().trim().equalsIgnoreCase(userInput)) {
                    // do something
                } else {
                    // do something
                }

What I would like to do is add another feature in here that also ignores spaces before, between and after text e.g. instead of;

   myname   is Henry      .   (space until here)

It should read it as mynameishenry but to the user it still appears as they have written it.

Please let me know if my question was not clear, I will try explaining it better

EDITED:

is it possible to ignore spaces in string that I have inside my if statement. For example;

if (myText.getText().toString().trim().equalsIgnoreCase("Henry 0887")) {
                    // do something
                } else {
                    // do something
                }

but currently if the user types in henry0887, the if statement does not validate it because I added a space inside my validation text and therefoe its looking for a space in the text, is it possible to over come this, so even if I have space inside my validation it ignores it.


Solution

  • Did you try this:

    String  myString = myEditText.getText().toString();
    myString  = myString .replace(" ", "");
    

    Hope it helps

    EDIT:

    if (myText.getText().toString().replace(" ", "").equalsIgnoreCase(userInput) || myText.getText().toString().equalsIgnoreCase(userInput)) {...