追加和替换字符串无效

时间:2019-06-10 11:53:03

标签: java android string append

我正在尝试将所有空格替换为“”,并将@ email.com附加到字符串中,我的输入是用户名和密码 我在Click侦听器上拥有一个功能,当单击Button时,它应该操纵文本并将其保存到数据库中

这是我的代码:

    musername = findViewById(R.id.username);
        memail = findViewById(R.id.email);
        mfullname = findViewById(R.id.fullname);
        mpassword = findViewById(R.id.password);
        register = findViewById(R.id.register);
        txt_login = findViewById(R.id.txt_login
    public void onClick(View view) {
                pd = new ProgressDialog(RegisterActivity.this);
                pd.setMessage("Please wait...\nWaiting too long? Please try another nickname");
                pd.show();
                //Testiram Register Sistem
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(musername.getText().toString().replaceAll("\\s+","")).append("@email.com");
                email = stringBuilder.toString();
                String semail=email;
                String str_username = musername.getText().toString();
                String str_fullname = musername.getText().toString();
                String str_password = mpassword.getText().toString();




                register(semail,str_username, str_fullname, str_password);

            }
        });
    }

    public void register(final String username, final String fullname,final String email, String password){
        auth.createUserWithEmailAndPassword(email, password

)

2 个答案:

答案 0 :(得分:1)

StringBuilder代码是正确的,但是如果您想从editText中获取文本并尝试使其为Email。因此,您应该在任何事件上执行此操作。例如,您可以单击一个按钮,然后单击该按钮可以发送电子邮件。

 createEmail.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(musername.getText().toString().replaceAll("\\s+","")).append("@email.com");
            email = stringBuilder.toString();
            String semail=email;
            String str_username = musername.getText().toString();
            String str_fullname = musername.getText().toString();
            String str_password = mpassword.getText().toString();

            register(str_username,str_fullname, semail,str_password);
    });

编辑替换

stringBuilder.append(musername.getText().toString().replaceAll("\\s+"," @email.com"));

stringBuilder.append(musername.getText().toString().replaceAll("\\s+","")).append("@email.com");

答案 1 :(得分:-1)

    String semail=email+"@email.com";

希望此行中添加“ @ email.com”。