Java掩碼的幾種使用例舉
2019-06-23 11:18Alan_阿蘭 Java教程
今天小編就為大家分享一篇關于Java掩碼的使用,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
java掩碼
01 | private static String nameMask(String name) throws Exception { |
02 | if (name == null ) throw new Exception( "請輸入要掩碼的字符串" ); |
03 | if (name.length()<= 1 ) return name+ "*" ; |
04 | return name.replaceAll( "([\\u4e00-\\u9fa5]{1})(.*)" , "$1" +createAsterisk(name.length()- 1 )); |
06 | private static String createAsterisk( int len) { |
07 | StringBuffer sb = new StringBuffer(); |
08 | for ( int i= 0 ;i<len;i++){ |
05 | public static String maskCertId(String certId) throws Exception |
07 | if (certId== null ||certId.length()== 0 ) return "" ; |
08 | if (certId.length()== 18 ) |
10 | String v = certId.substring( 0 , 4 ); |
11 | String end = certId.substring(certId.length()- 4 ); |
12 | return v+StringUtils.repeat( "*" , 8 )+end; |
03 | * @throws JBOException |
05 | public static String maskUserName(String userName) throws Exception |
07 | if (userName== null ||userName.length()== 0 ) return "" ; |
08 | String v = userName.substring( 0 , 1 ); |
09 | return StringUtils.rightPad(v, userName.length(), "*" ); |
04 | * @param startLength 被保留的開始長度 0代表不保留 |
05 | * @param endLength 被保留的結束長度 0代表不保留 |
08 | public static String wordMask(String word, int startLength , int endLength,String pad) { |
09 | if (word== null ) return StringUtils.leftPad( "" , startLength+endLength,pad); |
10 | if (word.length()<=startLength+endLength) return StringUtils.leftPad( "" , startLength+endLength,pad); |
14 | if (word.length()>startLength) startStr = StringUtils.substring(word, 0 ,startLength); |
15 | if (word.length()>startLength+endLength) endStr = StringUtils.substring(word, word.length()-endLength); |
16 | padLength = word.length()-startLength-endLength; |
17 | return startStr + StringUtils.repeat(pad, padLength)+endStr; |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。