Ad

Appending And Replacing String Doesn't Work

- 1 answer

I am trying to replace all spaces to "" and append @email.com to a String, my input is Username and Password I have on Click listener that when Button is clicked it should manipulate the text and save it into the database

Here is my code:

    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

)
Ad

Answer

StringBuilder code is correct but If you want to fetch text from your editText and trying to make it Email. So you should do it on any event. For example you can take a button and on its click you can make your 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);
    });

EDIT Relace

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

with

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