• java,名称工具类。手机号加星。


    package com.smart.common.util;
    /**
     * @author leslie
     * @Description
     */
    public class NameUtil {
    
        /**
         * 定义所有常量
         */
        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 字符串向左截取
         * @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 字符串向右截取
         * @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 根据不同名字的长度返回不同的显示数据
         * @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 "*" + NameUtil.right(str, ONE);
            }else if(str.length() == THREE){
                return NameUtil.left(str, ONE) + "*" + NameUtil.right(str, ONE);
            }else if(str.length() == FOUR){
                return NameUtil.left(str, ONE) + "**" + NameUtil.right(str, ONE);
            }
            return str;
        }
    
        /**
         * 测试
         */
        public static void main(String[] args) {
            System.out.println("名字: " + NameUtil.checkNameLength("海"));
            System.out.println("名字: " + NameUtil.checkNameLength("贼王"));
            System.out.println("名字: " + NameUtil.checkNameLength("海贼王"));
            System.out.println("名字: " + NameUtil.checkNameLength("大海贼王"));
            System.out.println("手机号: " + NameUtil.left("15838883888", THREE) + "*****" + NameUtil.right("15838883888", FOUR));
            System.out.println("信用卡号16位: " + NameUtil.left("1234567891011121", FOUR) + "*****" + NameUtil.right("1234567891011121", FOUR));
            System.out.println("银行卡号19位: " + NameUtil.left("1234567891011121314", FOUR) + "*****" + NameUtil.right("1234567891011121314", FOUR));
        }
    }
    
    

    main函数可以直接执行。

    名字: 海
    名字: *王
    名字: 海*王
    名字: 大**王
    手机号: 158*****3888
    信用卡号16位: 1234*****1121
    银行卡号19位: 1234*****1314
    
  • 相关阅读:
    Chrome自带恐龙小游戏的源码研究(四)
    Chrome自带恐龙小游戏的源码研究(三)
    Chrome自带恐龙小游戏的源码研究(二)
    Chrome自带恐龙小游戏的源码研究(一)
    使用HTML5制作简单的RPG游戏
    EventListener中的handleEvent
    canvas drawImage方法不显示图片的解决方案
    canvas转盘抽奖的实现(二)
    股市高手的领悟
    《最伟大的投资习惯》读书笔记
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/14468730.html
Copyright © 2020-2023  润新知