gpt4 book ai didi

java - 我正在编写一个用 HTML 标签替换特殊字符的 TextFormatter

转载 作者:行者123 更新时间:2023-11-28 01:21:46 25 4
gpt4 key购买 nike

我正在编写一个 TextFormatter,用 HTML 标签替换特殊字符。

"_" = "< i >" and "< /i >"
"*" = "< b >" and "< /b >"

所以..我的代码如下..

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;
}

我需要帮助的是 HTML 中的嵌套标签会导致错误。我不明白如何在 HTML 中正确嵌套标签,因为如果我在插入新标签之前不关闭标签,则会导致错误。我知道我的措辞方式可能会让它有点困惑,但我将不胜感激任何帮助,如果我能回答任何问题以消除任何困惑,请告诉我。

提前致谢! - 维夏尔

最佳答案

假设您的标记文本是正确的:

/**
*@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;
}

关于java - 我正在编写一个用 HTML 标签替换特殊字符的 TextFormatter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33685145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com