简体   繁体   English

Java:replaceAll和.split换行符不起作用

[英]Java : replaceAll and .split newline doesn't work

I am wanting to split a line up (inputLine) which is 我想将一行(inputLine)

Country: United Kingdom
City: London

so I'm using this code: 所以我在用这段代码:

public void ReadURL() {

    try {
        URL url = new URL("http://api.hostip.info/get_html.php?ip=");
        BufferedReader in = new BufferedReader(
        new InputStreamReader(url.openStream()));

        String inputLine = "";
        while ((inputLine = in.readLine()) != null) {
            String line = inputLine.replaceAll("\n", " ");
            System.out.println(line);
        }

        in.close();
    }   catch ( Exception e ) {
        System.err.println( e.getMessage() );
    }
}

when you run the method the the output is still 当您运行该方法时,输出仍然

Country: United Kingdom
City: London

not like it's ment to be: 不喜欢这样:

Country: United Kingdom City: London

now i've tried using 现在我已经尝试使用

\n,\\n,\r,\r\n

and

System.getProperty("line.separator")

but none of them work and using replace , split and replaceAll but nothing works. 但是它们都不起作用,不能使用replacesplitreplaceAll但是什么也没有。

so how do I remove the newlines to make one line of a String? 那么如何删除换行符以使字符串的一行呢?

more detail: I am wanting it so I have two separate strings 详细信息:我想要它,所以我有两个单独的字符串

String Country = "Country: United Kingdom";

and

String City = "City: London";

that would be great 那很好啊

You should instead of using System.out.println(line); 您应该代替使用System.out.println(line); use System.out.print(line); 使用System.out.print(line); .

The new line is caused by the println() method which terminates the current line by writing the line separator string. 新行是由println()方法引起的,该方法通过写入行分隔符字符串来终止当前行。

http://docs.oracle.com/javase/1.5.0/docs/api/java/io/BufferedReader.html#readLine() http://docs.oracle.com/javase/1.5.0/docs/api/java/io/BufferedReader.html#readLine()

Read that. 阅读。 readLine method will not return any carriage returns or new lines in the text and will break input by newline. readLine方法不会在文本中返回任何回车符或换行符,并且会用换行符中断输入。 So your loop does take in your entire blob of text but it reads it line by line. 因此,循环的确吸收了整个文本块,但它逐行读取。

You are also getting extra newlines from calling println. 您还可以通过调用println获得换行符。 It will print your line as read in, add a new line, then print your blank line + newline and then your end line + newline giving you exactly the same output as your input (minus a few spaces). 它将以读入状态打印您的行,添加新行,然后打印空白行+换行符,然后打印结束行+换行符,从而为您提供与输入完全相同的输出(减去几个空格)。

You should use print instead of println. 您应该使用print而不是println。

I would advise taking a look at Guava Splitter.MapSplitter 我建议看看Guava Splitter.MapSplitter

In your case: 在您的情况下:

// input = "Country: United Kingdom\nCity: London"
final Map<String, String> split = Splitter.on('\n')
    .omitEmptyStrings().trimResults().withKeyValueSeparator(": ").split(input);
// ... (use split.get("Country") or split.get("City")

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

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