开发者

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 t开发者_开发问答he String ?


Isn't that just Java?

String k = edittext.getText().toString().replace(" ", "");


try this...

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

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

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

and use "newData"


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.


You can easily remove all white spaces using something like this. But you'll face another serious problem if you just do that. For example if you have input

String input1 = "aa bb cc"; // output aabbcc
String input2 = "a abbcc";  // output aabbcc
String input3 = "aabb cc";  // output aabbcc

One solution will be to fix your application to accept white spaces in input string or use some other literal to replace the white spaces. If you are using only alphanumeric values you do something like this

String input1 = "aa bb cc"; // aa_bb_cc
String input2 = "a abbcc";  //a_abbcc
String input3 = "aabb cc";  //aabb_cc

And after all if you are don' caring about the loose of information you can use any approach you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜