方法一:循环,如下,如果 Dateof 这个参数是固定是可以只用一维数组
public String test(String fullText){
String testDate = 你的那个方法(fullText,"检定日期","Dateof");
final String[][] arr = new String[][]{{"检定日期","Dateof"},{"2","Dateof"},{"3","Dateof"},{"4","Dateof"},{"5","Dateof"}};
for (String[] strings : arr) {
if(StringUtils.isEmpty( testDate )){
testDate = 你的那个方法(fullText,strings[0],strings[1]);
}
}
return testDate;
}
public String metrologyInstituteCertificateExtract(String fullText) {
int i = 1;
String testdate = null;
while(StringUtils.isEmpty(testdate)) {
testdate = AnalyticalUtils.fillInCertificate(fullText, TextEnum.getNumTextPrefix(i).getTextPrefix(), TextEnum.getNumTextPrefix(i).getTextualSuffix());
i++;
}
return testdate;
}
@Getter
public enum TextEnum {
a1(1,"检定日期", "Dateof"),
a2(2,"检定日期", "Dateof"),
a3(3,"2", "Dateof"),
a4(4,"3", "Dateof"),
a5(5,"4", "Dateof"),
a6(6,"5", "Dateof"),
;
private Integer num;
private String textPrefix;
private String textualSuffix;
TextEnum(Integer num, String textPrefix, String textualSuffix) {
this.num = num;
this.textPrefix = textPrefix;
this.textualSuffix = textualSuffix;
}
public static TextEnum getNumTextPrefix(Integer num) {
if (null == num) {
return null;
}
for (TextEnum type:TextEnum.values()) {
if (type.getNum() == num) {
return type;
}
}
return null;
}
}
textPrefix 参数每次都不同,你可以把上面的判断封装成递归的形式,根据递归次数不同来改变后面两个参数的入参,最后要确定跳出递归的条件。
你下面的判空return是不是写错了,按逻辑应该是判定非空吧,而且应该放到循环里去,不然赋值必定是最后一次循环。
递归:
private final String[] textPrefixs = {"检定日期", "2","3","4","5"};
private String check(String fullText, int idx) {
if (!isEmpty(fullText) || idx >= textPrefixs.length) return fullText;
return check(fillInCertificate(fullText, textPrefixs[idx], "Dateof"), idx + 1);
}