Mask String with characters

后端 未结 5 1597
悲哀的现实
悲哀的现实 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:33

    My implementation:

    public static String maskString(String s, int x) {
        int n = s.length()/x;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
          if (n >= 1 && (i < n || i >= (s.length() - n))) {
            sb.append(s.charAt(i));
          }
          else {
            sb.append("*");
          }
        }
        return sb.toString();
      }
    

提交回复
热议问题