java实现姓名、手机号和银行卡中间用*号代替

本文介绍了一个用于模拟各大网站充值时的数据显示工具。通过定义一系列实用的方法,如字符串左右截取及根据名字长度调整显示数据等,实现了对敏感信息的有效保护。
package com.util.date;


/**
 * @Description 模拟各大网站充值时的数据显示
 * @author ShengLiu
 * @date 2018/7/4
 */
public class TestUtil {

    /**
     * 定义所有常量
     */
    public static final String EMPTY = "";
    public static final int ZERO = 0;
    public static final int ONE = 1;
    public static final int TWO = 2;
    public static final int THREE = 3;
    public static final int FOUR = 4;

    /**
     * @Description 字符串向左截取
     * @author ShengLiu
     * @date 2018/7/4
     * @param str
     * @param len
     * @return java.lang.String
     */
    public static String left(String str, int len) {
        if (str == null) {
            return null;
        }
        if (len < ZERO) {
            return EMPTY;
        }
        if (str.length() <= len) {
            return str;
        }
        return str.substring(ZERO, len);

    }

    /**
     * @Description 字符串向右截取
     * @author ShengLiu
     * @date 2018/7/4
     * @param str
     * @param len
     * @return java.lang.String
     */
    public static String right(String str, int len) {
        if (str == null) {
            return null;
        }
        if (len < ZERO) {
            return EMPTY;
        }
        if (str.length() <= len) {
            return str;
        }
        return str.substring(str.length() - len);

    }

    /**
     * @Description 根据不同名字的长度返回不同的显示数据
     * @author ShengLiu
     * @date 2018/7/4
     * @param str
     * @return java.lang.String
     */
    public static String checkNameLength(String str){
        if(str == null){
            return null;
        }
        if(str.length() == ONE) {
            return str;
        }else if(str.length() == TWO){
            return "*" + TestUtil.right(str, ONE);
        }else if(str.length() == THREE){
            return TestUtil.left(str, ONE) + "*" + TestUtil.right(str, ONE);
        }else if(str.length() == FOUR){
            return TestUtil.left(str, ONE) + "**" + TestUtil.right(str, ONE);
        }
        return str;
    }

    /**
     * 测试
     */
    public static void main(String[] args) {
        System.out.println("名字: " + TestUtil.checkNameLength("海"));
        System.out.println("名字: " + TestUtil.checkNameLength("贼王"));
        System.out.println("名字: " + TestUtil.checkNameLength("海贼王"));
        System.out.println("名字: " + TestUtil.checkNameLength("大海贼王"));
        System.out.println("手机号: " + TestUtil.left("15838883888", THREE) + "*****" + TestUtil.right("15838883888", FOUR));
        System.out.println("信用卡号16位: " + TestUtil.left("1234567891011121", FOUR) + "*****" + TestUtil.right("1234567891011121", FOUR));
        System.out.println("银行卡号19位: " + TestUtil.left("1234567891011121314", FOUR) + "*****" + TestUtil.right("1234567891011121314", FOUR));
    }

}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值