识别下面行中重复代码的问题
problem in recognizing duplicated code in the line bellow
我找不到代码的哪一部分重复以及如何修复它?
try {
String template = new String(sourceTemplate);
// Substitute for %CODE%
int templateSplitBegin = template.indexOf("%CODE%");
int templateSplitEnd = templateSplitBegin + 6;
String templatePartOne = new String(
template.substring(0, templateSplitBegin));
String templatePartTwo = new String(
template.substring(templateSplitEnd, template.length()));
code = new String(reqId);
template = new String(templatePartOne + code + templatePartTwo);
// Substitute for %ALTCODE%
templateSplitBegin = template.indexOf("%ALTCODE%");
templateSplitEnd = templateSplitBegin + 9;
templatePartOne = new String(
template.substring(0, templateSplitBegin));
templatePartTwo = new String(
template.substring(templateSplitEnd, template.length()));
altcode = code.substring(0, 5) + "-" + code.substring(5, 8);
out.print(templatePartOne + altcode + templatePartTwo);
} catch (Exception e) {
System.out.println("Error in substitute()");
}
这两部分是重复的。你可以创建一个方法来处理字符串
try {
String template = new String(sourceTemplate);
// Substitute for %CODE%
code = new String(reqId);
template = processString(template, 6, "%CODE%", code);
// Substitute for %ALTCODE%
altcode = code.substring(0, 5) + "-" + code.substring(5, 8);
template = processString(template, 9, "%ALTCODE%", altcode);
out.print(template);
} catch (Exception e) {
}
private String processString(String template, int length, String code, String mid) {
int templateSplitBegin = template.indexOf(code);
int templateSplitEnd = templateSplitBegin + len;
String templatePartOne = new String(
template.substring(0, templateSplitBegin));
String templatePartTwo = new String(
template.substring(templateSplitEnd, template.length()));
return new String(templatePartOne + mid + templatePartTwo);
}
尝试创建一个以 (String template, String splitBy, int offset)
作为参数的方法。
此外,您可以尝试使用 stringBegin = template.split(splitBy)[0]
和 stringEnd = template.split(splitBy)[1]
而不是使用 indexOf()
然后 substring()
我找不到代码的哪一部分重复以及如何修复它?
try {
String template = new String(sourceTemplate);
// Substitute for %CODE%
int templateSplitBegin = template.indexOf("%CODE%");
int templateSplitEnd = templateSplitBegin + 6;
String templatePartOne = new String(
template.substring(0, templateSplitBegin));
String templatePartTwo = new String(
template.substring(templateSplitEnd, template.length()));
code = new String(reqId);
template = new String(templatePartOne + code + templatePartTwo);
// Substitute for %ALTCODE%
templateSplitBegin = template.indexOf("%ALTCODE%");
templateSplitEnd = templateSplitBegin + 9;
templatePartOne = new String(
template.substring(0, templateSplitBegin));
templatePartTwo = new String(
template.substring(templateSplitEnd, template.length()));
altcode = code.substring(0, 5) + "-" + code.substring(5, 8);
out.print(templatePartOne + altcode + templatePartTwo);
} catch (Exception e) {
System.out.println("Error in substitute()");
}
这两部分是重复的。你可以创建一个方法来处理字符串
try {
String template = new String(sourceTemplate);
// Substitute for %CODE%
code = new String(reqId);
template = processString(template, 6, "%CODE%", code);
// Substitute for %ALTCODE%
altcode = code.substring(0, 5) + "-" + code.substring(5, 8);
template = processString(template, 9, "%ALTCODE%", altcode);
out.print(template);
} catch (Exception e) {
}
private String processString(String template, int length, String code, String mid) {
int templateSplitBegin = template.indexOf(code);
int templateSplitEnd = templateSplitBegin + len;
String templatePartOne = new String(
template.substring(0, templateSplitBegin));
String templatePartTwo = new String(
template.substring(templateSplitEnd, template.length()));
return new String(templatePartOne + mid + templatePartTwo);
}
尝试创建一个以 (String template, String splitBy, int offset)
作为参数的方法。
此外,您可以尝试使用 stringBegin = template.split(splitBy)[0]
和 stringEnd = template.split(splitBy)[1]
而不是使用 indexOf()
然后 substring()