简体   繁体   中英

String replace is not printing the value in console

I've an HTML File as below

<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.

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.

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

Thanks

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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