1、添加依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
2、工具测试类
package com.shucha.deveiface.biz.test;
import org.apache.commons.lang3.StringUtils;
import java.io.UnsupportedEncodingException;
/**
* @author tqf
* @Description 字符串补全、截取
* @Version 1.0
* @since 2022-07-15 09:47
*/
public class Completion {
public static void main(String[] args) {
// 2个字符串补全长度一样
lengthStr("2587发","192.168.0.125"," ");
String str = "字符串补全和截取的方法测试";
System.out.println("左侧补全:" + completionStr(str,18, "*",0,0));
System.out.println("中间补全:" + completionStr(str,18, "*",0,1));
System.out.println("右边补全:" + completionStr(str,18, "*",0,2));
System.out.println("左侧截取:" + completionStr(str,2, "*",1,0));
System.out.println("中间截取:" + completionStr(str,5, "*",1,1));
System.out.println("右边截取:" + completionStr(str,6, "*",1,2));
System.out.println("手机号脱敏:" + wordMask("12365485744",3,4,"*"));
System.out.println("身份证号码脱敏:" + wordMask("340604199908022750",4,4,"*"));
}
/**
* 2个字符串比较长度 添加字符补全一样的长度
* @param oneStr 第一个字符串
* @param twoStr 第二个字符串
* @param padStr 补全的字符
*/
public static void lengthStr(String oneStr, String twoStr, String padStr){
try {
int beforeLength = oneStr.getBytes("gbk").length;
int afterLength = twoStr.getBytes("gbk").length;
int gapSize = beforeLength - afterLength;
if (gapSize > 0) {
twoStr = StringUtils.rightPad(twoStr, gapSize + twoStr.length(), padStr);
}
if (gapSize < 0) {
oneStr = StringUtils.rightPad(oneStr, Math.abs(gapSize) + oneStr.length(), padStr);
}
System.out.println(oneStr);
System.out.println(twoStr);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/**
* 字符串-补全、截取
* @param string 原始字符串
* @param length 字补全后的符串长度
* @param padStr 补充的字符串
* @param type 类型 0-补全字符串,1-截取字符串
* @param padType 补全的方式 0-左侧补齐,1-中间补齐,2-右侧补齐
* @return
*/
public static String completionStr(String string, int length, String padStr, int type, int padType){
String str = "";
if(type == 0){
switch (padType) {
case 0:
str = StringUtils.leftPad(string, length, padStr);
break;
case 1:
str = StringUtils.center(string, length, padStr);
break;
case 2:
str = StringUtils.rightPad(string, length, padStr);
break;
default:
str = "";
}
} else {
switch (padType) {
case 0:
str = StringUtils.left(string, length);
break;
case 1:
str = StringUtils.center(string, length);
break;
case 2:
str = StringUtils.right(string, length);
break;
default:
str = "";
}
}
return str;
}
/**
* 对字符串进行脱敏处理
* @param word 被脱敏的字符
* @param startLength 被保留的开始长度 前余n位
* @param endLength 被保留的结束长度 后余n位
* @param pad 填充字符
*/
public static String wordMask(String word, int startLength, int endLength, String pad) {
if (StringUtils.isAnyBlank(word, pad)) {
return word;
}
if (startLength + endLength > word.length()) {
return StringUtils.leftPad("", word.length() - 1, pad);
}
String startStr = word.substring(0, startLength);
String endStr = word.substring(word.length() - endLength, word.length());
return startStr + StringUtils.leftPad("", word.length() - startLength - endLength, pad) + endStr;
}
}
3、测试结果如图
![]()