Mask String with characters

后端 未结 5 1601
悲哀的现实
悲哀的现实 2021-01-12 10:46

Hey guy\'s I tried to find a way to hide a string, but the code that I found just work with my application... Is there a way to hide the characters in a string with either <

5条回答
  •  情书的邮戳
    2021-01-12 11:14

    There are several ways to achieve this, it would depend on your application.

    If you want to mask all characters with another character in one fell swoop you can use the String#replaceAll(String regex, String replacement) method: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String).

    This involves using Regular Expressions, for regex you would use [\s\S] which will match any whitespace or non whitespace character. For replacement you use a regular string, not a RegEx. In this case, if you wanted an asterisk, use "*", for hyphen "-", very simple.

    All the other methods here work well except the @Roddy of the Frozen Pea and @djc391 ones, so that's why I answered it correctly.

    Good luck

提交回复
热议问题