[英]Strange String.split("\n") behavior
我有一個 .txt 文本文件,其中包含一些行。我使用 RequestBuilder 對象加載包含,並使用 words = String.split("\\n"); 拆分 responseText; 但我想知道,為什么結果包含“\\n”部分。例如,我的文字:
abc
def
ghi
結果是,
words[0] = "abc\n"
words[1] = "def\n"
words[2] = "ghi\n"
任何幫助都受到高度贊賞。 提前致謝。
嘗試使用string.split("\\\\n+")
。 甚至更好split("[\\\\r\\\\n]+")
您可能還需要考慮String[] lines = text.split("\\\\\\\\n");
如果split使用正則表達式,則應使用“ \\\\ n”而不是“ \\ n”
嘗試使用string.split("\\\\\\\\n")
。 這個對我有用。
也許這很簡單,但是 .split 方法對空格和文本中斷很敏感。 如果我們不知道原文是如何寫的,我們必須考慮這可能會產生一些差異(單行、中斷、多行等)。
單行文字:
const inlineText = "Hello world!";
console.log(inlineText.split(' ')) //['Hello', 'world!']
多行文字:
const multilinesText = `
Hello world!
`
console.log(multilinesText.split(' ')) // ['\nHello', 'world!', '\n']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.