小编典典

如何在Jsp中比较两个字符串?

jsp

if(student_code.substring(0,3 )=="MLV")
  count1++;

count1总是返回0


阅读 606

收藏
2020-06-10

共1个答案

小编典典

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中的下面的taglibs中导入

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
2020-06-10