如何把<,>转换成<,>.给出代码???

southline 2001-10-25 10:39:45
麻烦大虾给出具体代码。
如原来的格式是<html><body>ddfd</body></html>这是从数据库里查询出的内容,
把它转换成<html><body></body></html>
帮忙。
...全文
587 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lgcfm 2001-10-27
  • 打赏
  • 举报
回复
public String formatHTML(String input)
{ if(input==null||input.length() ==0){
return input;}
//建立一个Stringbuffer 来处理输入的数据
StringBuffer buf=new StringBuffer(input.length() +6);
char ch=' ';
//处理非法字符穿
for (int i=0;i<input.length() ;i++){
ch=input.charAt(i);
if(ch=='<') {
buf.append("<");
}
if (ch=='>'){
buf.append(">");
}
if (ch=='\n'){
buf.append("<br>");
}
if(ch=='\''){
buf.append("´");
}
if (ch==' '){
buf.append(" ");
}
else
{
buf.append(ch);
}
}
return buf.toString();
}

可以给我结帐了
kcb111 2001-10-26
  • 打赏
  • 举报
回复
为什么还不结帐???
kcb111 2001-10-25
  • 打赏
  • 举报
回复
函数:
<%!
public String replace(String line,String ch,String rep)
{
int i = line.indexOf(ch);
StringBuffer sb = new StringBuffer();
if (i == -1)
return line;
sb.append(line.substring(0,i) + rep);
if (i+ch.length() < line.length())
sb.append(replace(
line.substring(i+ch.length(),line.length()),
ch,
rep));
return sb.toString();
}
%>

////举例
try
{
fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(
new InputStreamReader(fis));
String line;
while ((line = br.readLine()) != null)
{
line = replace(line, "&", "&");
line = replace(line, "<", "<");
line = replace(line, ">", ">");
out.println(line);
}
fis.close();
}
catch (IOException e)
{
out.println("IOException: " + e.getMessage());
}
doli 2001-10-25
  • 打赏
  • 举报
回复
public static String change(String rString) {

int lIndex = 0;
char lChar ;
StringBuffer lStrbuff;

if (rString == null) return "";
lStrbuff = new StringBuffer(rString);

while (lIndex < lStrbuff.length()) {
if ((lChar = lStrbuff.charAt(lIndex)) == '"') {
lStrbuff.replace(lIndex, lIndex+1, """);
lIndex += 5;
continue;
}
else if (lChar == '%') {
lStrbuff.replace(lIndex, lIndex+1, "%25");
lIndex += 3;
continue;
}
else if (lChar == '&') {
lStrbuff.replace(lIndex, lIndex+1, "&");
lIndex += 5;
continue;
}
else if (lChar == '<') {
lStrbuff.replace(lIndex, lIndex+1, "<");
lIndex += 4;
continue;
}
else if (lChar == '>') {
lStrbuff.replace(lIndex, lIndex+1, ">");
lIndex += 4;
continue;
}
else if (lChar == '\n') {
lStrbuff.replace(lIndex, lIndex+1, "<br>");
lIndex += 4;
continue;
}
//空格
else if (lChar == '\u0020') {
lStrbuff.replace(lIndex, lIndex+1, " ");
lIndex += 6;
continue;
}


lIndex++;
}
return lStrbuff.toString();
}
zhjx_10 2001-10-25
  • 打赏
  • 举报
回复
public String replaceBeginMark(String s) {
while ( s!= null && (s.indexOf("<") != -1) ) {
s = s.substring(0,s.indexOf("<")) + "<" + s.substring(s.indexOf("<") + 1);
}
return s;
}

>同理

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧