Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing space from Edit Text String

In my android app, I am getting the String from an Edit Text and using it as a parameter to call a web service and fetch JSON data. Now, the method I use for getting the String value from Edit Text is like this :

final EditText edittext = (EditText) findViewById(R.id.search);

String k = edittext.getText().toString();

Now normally it works fine, but if we the text in Edit Text contains space then my app crashes.

for eg. - if someone types "food" in the Edit Text Box, then it's OK but if somebody types "Indian food" it crashes.

How to remove spaces and get just the String ?

like image 648
Ankit Avatar asked Aug 23 '26 10:08

Ankit


2 Answers

Isn't that just Java?

String k = edittext.getText().toString().replace(" ", "");
like image 188
Till Avatar answered Aug 25 '26 23:08

Till


try this...

final EditText edittext = (EditText) findViewById(R.id.search);

String k = edittext.getText().toString();

String newData  = k.replaceAll(" ", "%20");

and use "newData"

like image 39
Niranj Patel Avatar answered Aug 26 '26 00:08

Niranj Patel