简体   繁体   English

Java split()无法按预期工作

[英]Java split() not working as expected

I currently have text where I have "TweetJSONObject\\r\\n09/19/14TweetJSONObject" where the TweetJSONObject is just a tweet in json format. 我目前在“ TweetJSONObject \\ r \\ n09 / 19 / 14TweetJSONObject”中有文本,其中TweetJSONObject只是json格式的tweet。 Now I'm using the .split() function to try separate the tweets from one another but it would seem that \\r\\n09/19/14 isn't an appropriate split string? 现在,我使用.split()函数尝试将tweet彼此分开,但是\\ r \\ n09 / 19/14似乎不是合适的分割字符串? Here is the code: 这是代码:

String[] value = line.split("\r\n09/19/14");

PrintWriter writer = new PrintWriter("hello.txt", "UTF-8");
writer.println(value[0]);
writer.close();

The text file 'hello', when I open it is just the following string again, "TweetJSONObject\\r\\n09/19/14TweetJSONObject". 文本文件“ hello”,当我打开它时,它只是下面的字符串“ TweetJSONObject \\ r \\ n09 / 19 / 14TweetJSONObject”。 Any ideas as to where I'm going wrong? 关于我要去哪儿有什么想法吗?

You need to escape the backslashes: 您需要转义反斜杠:

String[] value = line.split("\\\\r\\\\n09/19/14");

Why so many backslashes? 为什么这么多反斜杠?

In literal Java strings the backslash is an escape character. 在文字Java字符串中,反斜杠是转义字符。 The literal string "\\\\" is a single backslash. 文字字符串"\\\\"是单个反斜杠。 In regular expressions, the backslash is also an escape character. 在正则表达式中,反斜杠也是转义字符。 The regular expression \\\\ matches a single backslash. 正则表达式\\\\匹配单个反斜杠。 So this regular expression as a Java string, becomes "\\\\\\\\" . 因此,此正则表达式作为Java字符串变为"\\\\\\\\"

I would recommend using the library method Pattern.quote instead of trying to escape stuff on your own (which is too confusing and error prone). 我建议使用库方法Pattern.quote而不要尝试自己逃脱(这太令人困惑且容易出错)。 A small runnable example: 一个小的可运行示例:

package net.sanjayts;

import java.util.regex.Pattern;

public class RegexTest {
    public static void main(String[] args) {
        String s = "TweetJSONObject\r\n09/19/14TweetJSONObject";
        String[] parts = s.split(Pattern.quote("\r\n09/19/14"));
        System.out.println(parts[0] + " --- " + parts[1]);
    }
}

//Output: TweetJSONObject --- TweetJSONObject

You need to escape the slashes 你需要逃脱斜线

String[] value = line.split("\\\\r\\\\n09/19/14");

PrintWriter writer = new PrintWriter("hello.txt", "UTF-8");
writer.println(value[0]);
writer.close();

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

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