Reputation: 693
I have a string which may or may not end with whitespace and I'm having trouble determining whether that's the case.
The reason why I am asking this is because I need a good way to calculate the width of the android since my string auto corrects and goes to the next line.
In android it looks like something like this:
The quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy d - my method substrings the data to fit on screen. |The quick brown fox jumps over the lazy | - this is how it would look like on android |dog---------------(empty space)----------|
Now to rephrase my question I want to know what method do I need to use to check the last character in my string is whitespace?
Upvotes: 1
Views: 3719
Reputation: 1626
In short:
boolean isSpace = yourString.charAt(yourString.length() - 1) == ' ';
EDIT: After reading the great comment from MH bellow, you should instead use:
boolean isSpace = Character.isWhitespace(yourString.charAt(yourString.length() - 1));
But if you want only to remove the space at the end, you can use the trim()
method on the String
class:
yourString = yourString.trim();
Upvotes: 2