Which is the best way to mask a String in Java?

Tito :

I'm want to apply a mask on a String "96506550" but I'm getting 965065-50 instead of 96506-550

//my method
public String format(String data, String mask) {
        MaskFormatter mf = new MaskFormatter(mask);        
        return mf.valueToString(data);
}

System.out.println(formatter.format("96506550","#####-###"));
Piyush Maheswari :

You forgot to use

public void setPlaceholderCharacter(char placeholder)

This function sets the character to use in place of characters that are not present in the value, ie the user must fill them in. The default value is a space. This is only applicable if the placeholder string has not been specified, or does not completely fill in the mask.

That's the reason there is a single character placeholder error.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=237432&siteId=1