Java 我正在编写一个文本格式化程序,用HTML标记替换特殊字符
我正在编写一个TextFormatter,用HTML标记替换特殊字符Java 我正在编写一个文本格式化程序,用HTML标记替换特殊字符,java,html,Java,Html,我正在编写一个TextFormatter,用HTML标记替换特殊字符 "_" = "< i >" and "< /i >" "*" = "< b >" and "< /b >" “quot=”“和“” *“=”“和“” 所以。。我的代码如下 public String convertBold() { if (countStrings("_") % 2 == 1) return 1; String tag =
"_" = "< i >" and "< /i >"
"*" = "< b >" and "< /b >"
“quot=”“和“”
*“=”“和“”
所以。。我的代码如下
public String convertBold() {
if (countStrings("_") % 2 == 1)
return 1;
String tag = "<b>";
String result = "";
while (find String("_", psn) >= 2) {
int newPsn = findString("_", psn);
// Copy the code before the "_" into the result
result = result + line.substring(psn, newPsn);
// Add the tag and change the tag
result = result + tag;
if (tag.equals("<B>"))
tag = "</B>";
else
tag = "<B>";
//update the psn
psn = newPsn++;
}
//copy the rest of the string
result = result + line.substring(psn);
return result;
}
公共字符串convertBold(){
if(countStrings(“”)%2==1)
返回1;
字符串标签=”;
字符串结果=”;
while(查找字符串(“25;”,psn)>=2){
int newPsn=findString(“,”psn);
//将“\”前面的代码复制到结果中
结果=结果+行.子字符串(psn,newPsn);
//添加标记并更改标记
结果=结果+标签;
if(标记为等于(“”)
tag=“”;
其他的
tag=“”;
//更新psn
psn=newPsn++;
}
//复制字符串的其余部分
结果=结果+行子字符串(psn);
返回结果;
}
我需要帮助的是,在HTML中嵌套标记可能会导致错误。我不知道如何在HTML中正确嵌套标记,因为如果在插入新标记之前不关闭标记,就会导致错误。我知道我的措辞可能会让人有点困惑,但我会感谢任何帮助,如果我能回答任何问题来消除任何困惑,请让我知道
提前谢谢你Veaxial假设您的标记文本是正确的:
/**
*@param s string to HTML
*
*/
String convert(String s){
while(s.indexOf("_")!= -1 ||s.indexOf("*") != -1){
if(s.indexOf("_") != -1){
s = s.replaceFirst("\\_", "<i>");
s = s.substring(0, s.lastIndexOf("_"))+"</i>"+s.substring(s.lastIndexOf("_")+1);
}
if(s.indexOf("*") != -1){
s = s.replaceFirst("\\*", "<b>");
s = s.substring(0, s.lastIndexOf("*"))+"</b>"
+s.substring(s.lastIndexOf("*")+1);
}
}//end while
return s;
}
/**
*@参数的字符串转换为HTML
*
*/
字符串转换(字符串s){
while(s.indexOf(“”)!=-1 | | s.indexOf(“*”)!=-1){
如果(s.indexOf(“”)!=-1){
s=s.replaceFirst(“\\\\”,“”);
s=s.substring(0,s.lastIndexOf(“”)+”+s.substring(s.lastIndexOf(“”)+1);
}
如果(s.indexOf(“*”)!=-1){
s=s.replaceFirst(“\\*”,“”);
s=s.substring(0,s.lastIndexOf(“*”)+“”
+s、 子字符串(s.lastIndexOf(“*”)+1);
}
}//结束时
返回s;
}