I have a edit text field in which reason can be entered. But if a person enters all spaces in the reason how can I detect it. Is there a way when a person e开发者_StackOverflownters all spaces or nothing , i have to save the text the text as a simple "hiphen". My main question is how to trim those spaces coming at the end.
The method trim() in String trims excess spaces on Strings.
String str = "Hello ";
String str2 = str.trim();
str2 would equal "Hello".
As for detecting when a person enters all spaces - check the str.length() after you've ran str.trim().
You would be handling some events too..
like whenever the user clicks a button, you collect the text of your edit text.
For your need, there's a function:
String String.trim();
it removes all the spaces that are before and after your text (leading and trailing spaces)
to use it, do this:
String msg = editText.getText().toString();
msg = msg.trim();
if(msg.equals("")){
msg = "-";
}
now 'msg' will be having a 'hyphen' in case user has entered nothing.
this is almost what @Laurence said..
精彩评论