字符串替换不起作用我认为应该 [英] String Replace not working as I think it should

查看:90
本文介绍了字符串替换不起作用我认为应该的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static boolean passwordConfirmed() {
    String attempt = JOptionPane.showInputDialog("Password: ");
    FileReader fstream = null;
    String password = "";
    try {

        fstream = new FileReader("pass.txt");
        BufferedReader in = new BufferedReader(fstream);

        password = in.readLine();
        password.replace("Password: ", " ");

        System.out.println(password);

    } catch (IOException e) {
        e.printStackTrace();
    }

    if (attempt.equals(password)) {
        System.out.print("True");
        return true; 
    } else System.out.println("false");
    return false;
}

尝试从该行中删除密码:。
得到密码:+后面的文字行(密码)
我想删除密码:,所以我剩下的就是之后的纯文字。

Trying to remove "Password: " from the line. It gets the line "Password: " + text afterwards (Password) I want to remove the "Password: ", so all i have left is purely the text afterwards.

推荐答案

始终重新分配。

password = password.replace("Password: ", " ");

字符串在Java中是不可变的,这意味着您无法修改它的现有实例。通过重新分配它,您将捕获字符串的新值到现有变量中。

Strings are immutable in Java, meaning you can't modify an existing instance of it. By re-assigning it, you'll be capturing the new value of the string into an existing variable.

这篇关于字符串替换不起作用我认为应该的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆