[英]How to find String.indexOf(“\”)?
I need to get the substring that appears before a bunch of \\r\\n\\r\\n junk. 我需要获取一堆\\ r \\ n \\ r \\ n垃圾之前出现的子字符串。 So I want to geto the first /that appears.
所以我想开始出现的第一个。 indexOf("\\") is not accepted.
indexOf(“ \\”)不被接受。 String.indexOf("\\") does not match the first .
String.indexOf(“ \\”)与第一个不匹配。 ?????
????? Thanks
谢谢
If you're looking for the first occurrence of \\n
in a string then searching for \\\\
won't help. 如果要在字符串中查找
\\n
的第一个匹配项,则搜索\\\\
将无济于事。 You need to look for \\n
. 您需要寻找
\\n
。
String.indexOf("\n")
Note that \\n
is one character, not two. 请注意,
\\n
是一个字符,而不是两个字符。 It's an escape sequence that evaluates to the character with ASCII value 10. 这是一个转义序列,其结果为ASCII值为10的字符。
String s1 = "LINE1\r\n\r\nLINE2";
Matcher matcher = Pattern.compile(".+").matcher(s1);
if (matcher.find()) {
System.out.println(matcher.group());
} else {
System.out.println("String contains no character other than \\n and \\r");
}
or a bit more perverted: 还是有些变态:
String s1 = "LINE1\r\n\r\nLINE2";
BufferedReader br = new BufferedReader(new StringReader(s1));
try {
System.out.println(br.readLine());
} finally {
br.close();
}
Output in both cases: LINE1
两种情况下的输出:
LINE1
indexOf("/")
should work fine. 应该工作正常。 Why do you say it is not?
你为什么说不是呢?
Why does indexOf('/')
not work? 为什么
indexOf('/')
不起作用? Does it return the wrong index? 它返回错误的索引吗? Forward slashes should be fine -- backward slashes (
\\
) are escape characters. 正斜杠应该可以-反斜杠(
\\
)是转义字符。
如果您要查找'\\',请使用.indexOf(“ \\”);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.