gpt4 book ai didi

java - 无法对数组类型 String[] 调用replace(String, String)

转载 作者:行者123 更新时间:2023-12-01 19:02:56 25 4
gpt4 key购买 nike

在尝试在 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>");
}

}

}
%>

最佳答案

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

文本是数组,不能对数组调用replace()

您需要首先从给出字符串的数组中获取字符串,然后对该字符串调用替换。

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

关于java - 无法对数组类型 String[] 调用replace(String, String),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11540181/

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