我需要得到在一堆\ r \ n \ r \ n垃圾之前出現的子字符串。所以我想要geto第一個/出現。 indexOf(「\」)不被接受。 String.indexOf(「\」)與第一個不匹配。 ????? 謝謝如何查找String.indexOf(「」)?
回答
如果您正在查找字符串中第一次出現\n
,那麼搜索\\
將無濟於事。你需要尋找\n
。
String.indexOf("\n")
請注意,\n
是一個字符,而不是兩個。這是一個轉義序列,其值爲ASCII值爲10的字符。
我會嘗試\ r謝謝 – Androider 2011-04-05 04:27:44
爲什麼indexOf('/')
無效?它會返回錯誤的索引嗎?正斜槓應該很好 - 反斜槓(\
)是轉義字符。
反斜槓是'\'。糾正它。 – 2011-04-05 04:20:44
是的,這是一個錯字。 – 2011-04-05 04:22:05
ok字符串是\ r \ n \ r \ n – Androider 2011-04-05 04:24:03
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");
}
或略偏變態:
String s1 = "LINE1\r\n\r\nLINE2";
BufferedReader br = new BufferedReader(new StringReader(s1));
try {
System.out.println(br.readLine());
} finally {
br.close();
}
輸出在這兩種情況下:LINE1
- 1. C#:String.IndexOf到FileStream.Seek
- 2. 的Java API String.indexOf
- 3. Java:使用String.indexOf()檢查撇號字符
- 4. StringIndexOutOfBoundsException:與String.substring()和String.indexOf()
- 5. LinQ WHERE string.Contains或string.IndexOf?
- 6. 串,String.IndexOf返回-1
- 7. 性能string.IndexOf OrdinalIgnoreCase的VS CurrentCultureIgnoreCase
- 8. C#string.IndexOf()返回意外值
- 9. 爲什麼String.indexOf在JavaScript中檢查失敗?
- 10. 如何「查找」查找數據
- 11. 如何查找Commons.web.mvc.precompiledviews.dll?
- 12. 如何查找R
- 13. 如何查找MavenProject?
- 14. 如何查找以
- 15. 如何查找表
- 16. 如何查找表
- 17. 如何查找wxWidgets?
- 18. 如何查找ViewByTag?
- 19. 如何查找Requiredfieldvalidator
- 20. 如何查找Xpath?
- 21. 如何查找硒
- 22. String.IndexOf方法的表達式樹
- 23. 集合/字符串.Contains vs集合/ string.IndexOf
- 24. 與Swift中的'String.indexOf(「text」)'函數類似
- 25. 什麼是string.indexOf(「*。」)在Javascript中做什麼?
- 26. 爲什麼我的string.indexof(char)更快?
- 27. String.indexOf函數的意外結果?
- 28. 獲取字符串前/ String.indexof在C#後
- 29. {{if}}中的string.indexof()函數jsviews構造
- 30. 矩投擲TypeError string.indexOf使用Backgrid時
你的意思是\ n嗎? \ n – sheki 2011-04-05 04:18:24
對不起\ r \ n \ r \ n是字符串 – Androider 2011-04-05 04:22:53
'String s =「hello \ r \ n」; s = s.replaceAll(「\\ r \\ n」,「」);' – 2011-04-05 04:26:40