在Java中屏蔽信用卡号 [英] masking a creditcard number in java

查看:147
本文介绍了在Java中屏蔽信用卡号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用字符'X'掩盖信用卡号字符串中的字符.我编写了以下两个函数.第二个函数使用commons.lang.StringUtils类.我试图找到两种情况下花费的时间

I tried to mask the characters in a creditcard number string using character 'X'.I wrote two functions as below .The second function uses commons.lang.StringUtils class .I tried to find the time it takes in both cases

public static String maskCCNumber(String ccnum){
        long starttime = System.currentTimeMillis();
        int total = ccnum.length();
        int startlen=4,endlen = 4;
        int masklen = total-(startlen + endlen) ;
        StringBuffer maskedbuf = new StringBuffer(ccnum.substring(0,startlen));
        for(int i=0;i<masklen;i++) {
            maskedbuf.append('X');
        }
        maskedbuf.append(ccnum.substring(startlen+masklen, total));
        String masked = maskedbuf.toString();
        long endtime = System.currentTimeMillis();
        System.out.println("maskCCNumber:="+masked+" of :"+masked.length()+" size");
        System.out.println("using StringBuffer="+ (endtime-starttime)+" millis");
        return masked;
    }

    public static String maskCCNumberCommons(String ccnum){
        long starttime = System.currentTimeMillis();
        int total = ccnum.length();
        int startlen=4,endlen = 4;
        int masklen = total-(startlen + endlen) ;
        String start = ccnum.substring(0,startlen);
        String end = ccnum.substring(startlen+masklen, total);
        String padded = StringUtils.rightPad(start, startlen+masklen,'X'); 
        String masked = padded.concat(end);
        long endtime = System.currentTimeMillis();
        System.out.println("maskCCNumber:="+masked+" of :"+masked.length()+" size");
        System.out.println("using Stringutils="+(endtime-starttime)+" millis");
        return masked;
    }

public static void ccNumberMaskingDemo() {
   String mcard1="5555555555554444";
   maskCCNumber(mcard1);
   maskCCNumberCommons(mcard1);
}

当我运行它时,我得到了这个结果

When I ran this ,I got this result

maskCCNumber:=5555XXXXXXXX4444 of :16 size
using StringBuffer=0 millis
maskCCNumber:=5555XXXXXXXX4444 of :16 size
using Stringutils=25 millis

我不明白为什么commons.StringUtils比第一个函数中的for loop + StringBuffer花费更多的时间.很明显,我使用api是错误的方式..

I can't understand why commons.StringUtils is taking more time than the for loop+StringBuffer in the first function.Obviously I am using the api ,the wrong way..

在这种情况下,有人可以建议如何正确使用此api吗?

Can someone advise how to use this api correctly, in this case?

推荐答案

首先,如果对这样一个短时间运行的代码进行测量,由于您的CPU/库/任何东西的最小时序分辨率,您通常无法获得准确的结果.提供(这意味着您通常会一遍又一遍地看到0ms或相同的小值).

Firstly, if you make measurements of such a short-running code, you often do not get accurate results due to the minimal timing resolution your CPU/library/whatever provides (which means you usually get to see 0ms or the same small value over and over).

第二,更重要的是,不要对此进行优化! 过早的优化是万恶之源" ,并且在您只有几毫秒想要优化的情况下,这种情况就被彻底浪费了.您甚至必须远程考虑优化这种简单的屏蔽方法之前,您必须屏蔽数百万张信用卡.

Second and more importantly, do not optimize this! "Premature optimization is the root of all evil" and in a case where you have only a few ms that you want to optimize the effort is thoroughly wasted. You would have to mask millions of credit cards before you should even remotely think about optimizing this simple mask method.

这篇关于在Java中屏蔽信用卡号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆