简体   繁体   English

奇怪的String.split(“ \\ n”)行为

[英]Strange String.split(“\n”) behavior

I tried to split name but none of the tries using split method worked 我试图拆分name但是使用split方法的尝试均无效

public void insertUpdate(DocumentEvent e) {
   String name="PU_Y"+
               "PU-x"+
               "Pu-z";

    split = name.split("\\r\\n");
    split = name.split("\r\n");
    split = name.split("[\r\n]+");
    split = name.split("[\\r\\n]+");
    split = name.split("\\r?\\n");
    // I tried using all the above to split above string but was not successful
}

Please help to split this string 请帮助拆分此字符串

Your string doesn't contain any line breaks at all. 您的字符串根本不包含任何换行符。 The fact that you've concatenated it on multiple lines doesn't do anything. 您已将其串联连接的事实并没有任何作用。 Your statement is equivalent to: 您的陈述等同于:

String name = "PU_Y" + "PU-x" + "Pu-z";

or 要么

String name = "PU_YPU-xPu-z";

You probably meant: 您可能的意思是:

String name = "PU_Y\r\n" +
              "PU-x\r\n" +
              "Pu-z";

Retest your regular expressions with that change. 用该更改重新测试您的正则表达式。

Your string has no linebreaks in it, every time you start a new line, you are concatenating strings as one statement. 您的字符串中没有换行符,每当您开始新行时,您就将字符串连接为一条语句。

Your string will be: 您的字符串将是:

"PU_YPU-xPu-z"

If you want newlines, put a \\r\\n in the String before you terminate the string on each line: 如果要换行,请在字符串中放置\\r\\n ,然后在每一行上终止该字符串:

String name="PU_Y\r\n"+
            "PU-x\r\n"+
            "Pu-z\r\n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM