Java String mask left

Description

Java String mask left


//package com.demo2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String s = "demo2s.com";
        int len = 2;
        char mask_ch = '*';
        System.out.println(maskLeft(s, len, mask_ch));
    }/* w  w w  .j  av a2  s.c om*/

    /**
     * Returns a string consisting of "s", with each of the first "len" characters
     * replaced by "mask_ch" character.
     */
    public static String maskLeft(String s, int len, char mask_ch) {
        if (len <= 0) {
            return s;
        }
        len = Math.min(len, s.length());
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            sb.append(mask_ch);
        }
        sb.append(s.substring(len));
        return sb.toString();
    }
}



PreviousNext

Related