【发布时间】:2015-11-01 08:56:03
【问题描述】:
if(student_code.substring(0,3 )=="MLV")
count1++;
但是count1总是返回0
【问题讨论】:
if(student_code.substring(0,3 )=="MLV")
count1++;
但是count1总是返回0
【问题讨论】:
你可以使用'=='符号来比较JSP中的两个字符串,我会在中间放空格并将双引号改为单引号,例如:
if(student_code.substring(0,3 ) == 'MLV')
希望这会有所帮助。
参考帖:How to compare two object variables in EL expression language?
【讨论】:
You can use the '==' symbols to compare two strings 应该带有免责声明。
if(student_code.substring(0,3 )=="MLV")
count1++;
这看起来不像 JSP 代码。它看起来更像是 JSP 中的 scriptlet,只不过是 java 代码。如果是这样的话,你仍然需要使用equals进行字符串比较,比如
if(student_code.substring(0,3 ).equals("MLV"))
count1++;
如果要在 JSP 中对字符串进行子串化和比较,请使用如下所示的 JSTL 函数
<c:set var="mystring" value="<%=student_code%>"/>
<c:if test="${fn:substring(mystring, 0, 3) == 'MLV'}">
<%count1++;%>
<c:if>
此外,为了使上述 JSTL 代码正常工作,您还需要在 JSP 中导入以下标记库
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
【讨论】:
哦,朋友们......我只是将我的代码更改为
str1=student_code.substring(0,3 ).trim();
if(str1.equals("YML"))
count1++;
..及其工作
【讨论】: