String html标签自动补齐
itext转pdf时需要补齐结束标签。
public static void replace(String html){
StringBuilder stringBuffer = new StringBuilder();
stringBuffer.append(html);
String subTag = "<input";
String endTag = "</input>";
String lastTag = ">";
int count = 0;
//记录找到的索引
int i = 0;
int j = 0;
while(true) {
if(html.indexOf(subTag, i) == -1) {
//找不到,跳出来
break;
}else {
//找到,计数
count ++;
//更新查找索引值,继续找
i = html.indexOf(subTag, i)+1;
j = html.indexOf(lastTag,i);
if (html.length()>=j+9){
String substring = html.substring(j + 1, j+9);
boolean strFlag = !endTag.equalsIgnoreCase(substring);
if (j > i && strFlag){
int c = (count -1)*endTag.length();
j = j+1+c;
stringBuffer.insert(j,endTag);
}
}
}
}
System.out.println(stringBuffer.toString());
}