[英]Finding Spaces, Newlines and Tabs with charAt()
I'm trying to check if there is a space, a newline or a tab at the current character location. 我正在尝试检查当前角色位置是否有空格,换行符或制表符。 Spaces work but tabs and newlines dont.
空格有效但选项卡和换行符不行。 Go figure, I'm using escapes for those, and just a regular space for a space... What's the correct way to find these at a location?
去看看,我正在为那些人使用逃生,只是一个空间的常规空间...在一个地方找到这些的正确方法是什么?
if(String.valueOf(txt.charAt(strt)).equals(" ") ||
txt.charAt(strt) == '\r' ||
txt.charAt(strt) == '\n' ||
txt.charAt(strt) == '\t') {
//do stuff
}
This works for me: 这对我有用:
char c = txt.charAt(strt);
if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
System.out.println("Found one at " + strt);
Yours works too, although it's a bit harder to follow. 你的作品也有效,虽然它有点难以理解。 Why it doesn't work for you I don't know - maybe the string is badly formed?
为什么它对你不起作用我不知道 - 也许这个字符串形成得很糟糕? Are you sure you actually have tabs and stuff in it?
你确定你真的有标签和东西吗?
It should work just fine, check your input string. 它应该工作正常,检查您的输入字符串。 Also, the space can be checked by comparing a blank space character.
此外,可以通过比较空白字符来检查空间。 Creating a new String object just for comparison is costly.
仅用于比较来创建新的String对象是昂贵的。
This regex [\\s] will do the work. 这个正则表达式[\\ s]将完成这项工作。 It matches the whitespace, Equivalent to [\\t\\n\\r\\f].
它与空白相匹配,相当于[\\ t \\ n \\ r \\ n]。
Looking at the docs for Editable
in android, it returns a char
. 查看android中
Editable
的文档,它返回一个char
。 Therefore ... 因此......
if (txt.charAt(strt) == ' ' ||
txt.charAt(strt) == '\r' ||
txt.charAt(strt) == '\n' ||
txt.charAt(strt) == '\t')
{
//do stuff
}
Will produce the expected result. 会产生预期的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.