问题描述
- 小弟做了一个方法,执行完毕还会再进入方法一次,求指教下原因
- 小弟把代码贴到了下面,我要迭代的替换字符串中的内容,替换完毕后,方法执行到 return sb.toString(); 应该是结束了,可是我的代码在运行的时候执行了return sb.toString(); 以后又进来执行一次,分析不出来原因,还请大家赐教,不胜感激:
private String parseToCk(String str) {
String s1 = str.trim();
/**- s1截取字符串开始部分*/if (s1.contains(""["") && s1.contains(""]"")) {s1 = str.substring(str.indexOf(""["") + 1 str.indexOf(""]"")).trim();}String s2 = """";if (s1.contains("""")) {s2 = s1.substring(0 s1.lastIndexOf("""") + 4).trim();}String s3 = """";if(s1.indexOf(""})>0){s3 = s1.substring(s1.indexOf(""{"") s1.indexOf(""})+2).trim();}else{s3 = s1.substring(s1.indexOf(""{"") s1.indexOf(""}"")+1).trim(); }String s8 = s1.substring(s2.length() + s3.length() s1.length()).trim();StringBuffer sb = new StringBuffer();sb.append(s2);if (s3.contains(""img"")) {sb.append(""
"");s3 = s3.substring(s3.indexOf("""":"""") + 3 s3.length()).trim();String s4 = s3.substring(0 s3.indexOf("""""")).trim();String s5 = s3.substring(s3.indexOf(""imgHeight"") s3.indexOf(""imgWidth"")).trim();s5 = s5.replace(""imgHeight""height"").trim();s5 = s5.replace(""""""").trim();s5 = s5.replace(""px"").trim();String s6 = s3.substring(s3.indexOf(""imgWidth"") s3.indexOf(""type"")).trim();s6 = s6.replace(""""""").trim();s6 = s6.replace(""px"").trim();String img = """";sb.append(img);sb.append("""");} else {sb.append(""
"");s3 = s3.substring(s3.indexOf("""":"""") + 3 s3.length()).trim();s3 = s3.substring(0 s3.indexOf("""""")).trim();sb.append(s3);sb.append("""");}if (s8.length() > 0 && s8.contains(""content"")) {sb.append(s8);parseToCk(sb.toString());}return sb.toString(); }
调用私有方法的代码如下:
public String saveOrUpdateJump() {
try {
forumChannelList = forumChannelMgr.getForumChannelInfoes(forumChannel);
if (forumTopic != null && forumTopic.getId() != null) {
forumTopic = forumTopicMgr.getForumTopicObject(forumTopic);
String contant="""";
if(forumTopic.getContent()!=null){
contant=parseToCk(forumTopic.getContent());
}
forumTopic.setContent(contant);
return SUCCESS;
} else {
return SUCCESS;
}
} catch (PomInfoException e) {
e.printStackTrace();
log.error(e.getMessage() e);
return ERROR;
}
} - s1截取字符串开始部分*/if (s1.contains(""["") && s1.contains(""]"")) {s1 = str.substring(str.indexOf(""["") + 1 str.indexOf(""]"")).trim();}String s2 = """";if (s1.contains("""")) {s2 = s1.substring(0 s1.lastIndexOf("""") + 4).trim();}String s3 = """";if(s1.indexOf(""})>0){s3 = s1.substring(s1.indexOf(""{"") s1.indexOf(""})+2).trim();}else{s3 = s1.substring(s1.indexOf(""{"") s1.indexOf(""}"")+1).trim(); }String s8 = s1.substring(s2.length() + s3.length() s1.length()).trim();StringBuffer sb = new StringBuffer();sb.append(s2);if (s3.contains(""img"")) {sb.append(""
解决方案
你这个函数是个嵌套函数,在函数最后有 sb.append(s8); parseToCk(sb.toString());
嵌套函数当然可能会出现重复调用了。