我试图在 FX 中的 TextArea
的 getText()
中找到 System.lineSeparator()
,但那是有点无关。无论如何,我有以下一批代码:
String newLine = System.lineSeparator();
String log = "Hello there!" + newLine + "I said hello!";
System.out.println(log);
上面的奇怪之处在于,即使它在控制台中输出良好,如下所示:
Hello there!
I said hello!
如果我执行System.out.println(log.indexOf(newLine);
,我会在控制台上打印-1
,这意味着它没有找到它。
如果我这样做 System.out.println(log.indexOf("\n");
它会返回 12
,这是正确的(因为 newLine 是第 12 个字符在日志
中)。
有人知道怎么回事吗?
最佳答案
发布上述评论作为答案:
如果我没记错的话,使用 Swing 和 JTextArea,.getText() 会将所有行分隔符转换为\n,无论平台如何。我不知道它在 FX 的 TextArea 中是否同样有效,但这可能是一个开始寻找的好地方。
关于java - String.indexOf() 找不到 System.lineSeparator();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22814036/