0

jsp で文字列を操作しようとしているときに、コードで string.repalace() メソッドを使用しましたが、次のエラーが永続的に発生します。

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 9 in the jsp file: /final2.jsp
Cannot invoke replace(String, String) on the array type String[]
6:       public void main (String str) {
7:      String [] text = StringUtils.substringsBetween(str,"#","#");
8:      for (int i=0; i<text.length;i++) {
9:        String newtext = text.replace("#"+text[i]+"#","<b>"+text[i]+"</b>");
10:          }
11: //blank line
12:     }


Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:     102)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:331)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:469)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

私のコードは次のとおりです。

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*"      errorPage="" %>
<%@page import="org.apache.commons.lang.StringUtils" %>

<%
 final class setext {
  public void main (String str) {
    String [] text = StringUtils.substringsBetween(str,"#","#");
    for (int i=0; i<text.length;i++) {
      String newtext = text.replace("#"+text[i]+"#","<b>"+text[i]+"</b>");
     }

}

}
%>
4

3 に答える 3

3
text.replace("#"+text[i]+"#","<b>"+text[i]+"</b>");

replace()テキストは配列です。配列を呼び出すことはできません

最初に文字列を与える配列から文字列を取得し、その文字列で置換を呼び出す必要があります。

String temp = text[i];
temp.replace("#"+text[i]+"#","<b>"+text[i]+"</b>");
于 2012-07-18T11:24:01.483 に答える
1

text文字列の配列です-おそらく次のことを意味しました:

String newtext = text[i].replace("#"+text[i]+"#","<b>"+text[i]+"</b>");

text[i]、テキスト配列内の文字列の 1 つです。

それを見ても、その声明は非常に理にかなっています。

于 2012-07-18T11:24:55.940 に答える
0

必要に応じて「#」を置き換えるだけの場合:

 public void main (String str) {
    String [] text = StringUtils.substringsBetween(str,"#","#");
    for (int i = 0, n = text.length ; i<n; i++) {
        text[i] = text[i].replace("#"+text[i]+"#", "<b>"+text[i]+"</b>");
 }

編集済み

  • for(..)にnを追加します
  • スニペットにストレスがかかっている場合は、パターン/マッチャーモデルを使用して「#」を置き換えることもできます。
于 2012-07-18T11:52:37.523 に答える