Replace '@' character in java android

I want to replace character '@'

and '.'

with '_'

from my string. I can replace char '.'

with '_'

, but cannot replace char '@'

.

public String getemailparsing(String email){
        String result="";
        char keong = 64;
        for(int i=0; i<email.length();i++){
            if(email.charAt(i) == '@' ){
                result = email.replace('@', '_'); //this is NOT working
            }else if(email.charAt(i) == '.'){
                result = email.replace('.', '_'); //this one is working
            }
        }
        return result;
    }

      

any idea to replace char '@' ...

+3


source to share


2 answers


public String getEmailParsing(String email){
    return email.replaceAll("[@.]+","_");
}

      



+1


source


Apply a small change as shown below. And you will get the desired result.



public String getemailparsing(String email) {
        String result = email;

        if (email.contains("@")) {
            result = result.replace('@', '_'); 
        }
        if (email.contains(".")) {
            result = result.replace('.', '_'); 
        }
        return result;
    }

      

+1


source







All Articles