字符串替换不在控制台中打印值

时间:2022-10-25 07:52:53

I've an HTML File as below

我有一个HTML文件如下

<html>
<head>
<title>Title</title>
<link rel="stylesheet" type="text/css" href="er:#css" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
</head>
<body class="text" id="text">
<div class="chapter">
<a id="page1" />
</div>
</body>
</html>

I'm trying to replace a id with id num and using the below code.

我正在尝试用id num替换id并使用下面的代码。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
public class Test {
    public static void main(String[] args) throws IOException {
        String input = "file:///C:/Users/users/file.html";

        URL url= new URL(input);
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            if (inputLine.contains("a id")) {
                inputLine.replace("a id", "?pb label =");
            }
            System.out.println(inputLine);
        }
        in.close();
        }
    }

But when I print it in the console, it doesn't replace the data. I get the below output.

但是当我在控制台中打印它时,它不会替换数据。我得到以下输出。

<html>

Title

the word is not getting replaced. When I did a debug, If-loop is entered in this case, the the data is not replaced in console.

这个词没有被取代。当我进行调试时,在这种情况下输入了If循环,数据不会在控制台中被替换。

please let me know where am I going wrong and how can I fix this.

请让我知道我哪里出错了,我该如何解决这个问题。

Thanks

1 个解决方案

#1


6  

String is immutable. You must assign the result of replace back to inputLine, since replace returns a new String :

字符串是不可变的。您必须将replace的结果分配回inputLine,因为replace返回一个新的String:

inputLine = inputLine.replace("a id", "?pb label =");

#1


6  

String is immutable. You must assign the result of replace back to inputLine, since replace returns a new String :

字符串是不可变的。您必须将replace的结果分配回inputLine,因为replace返回一个新的String:

inputLine = inputLine.replace("a id", "?pb label =");