我有一个字符串,它可能以空格结尾,也可能不以空格结尾,但我无法确定是否是这种情况。
我问这个问题的原因是因为我的字符串会自动更正并转到下一行,所以我需要一种计算 android 宽度的好方法。
在android中它看起来像这样:
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)----------|
现在重新表述我的问题,我想知道我需要使用什么方法来检查字符串中的最后一个字符是否为空格?
最佳答案
简而言之:
boolean isSpace = yourString.charAt(yourString.length() - 1) == ' ';
编辑: 阅读下面 MH 的精彩评论后,您应该改为使用:
boolean isSpace = Character.isWhitespace(yourString.charAt(yourString.length() - 1));
但是如果你只想删除末尾的空格,你可以使用 String
类的 trim()
方法:
yourString = yourString.trim();
关于android - 检查字符串的末尾是否为空白字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8824079/