如何在Jsp中比较两个字符串? [英] How could I compare two string in Jsp?

查看:883
本文介绍了如何在Jsp中比较两个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

但是count1总是返回0

推荐答案

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

这看起来不像JSP代码.它看起来更像是JSP中的scriptlet,不过就是Java代码.如果是这样,您仍然需要使用equals进行字符串比较,例如

This doesn't look like a JSP code. It looks more of a scriptlet in JSP, which is nothing but java code. If that's the case, you still need to use equals for string comparison, like

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

如果要在JSP中子字符串化和比较字符串,请使用如下所示的JSTL函数

If you want to substring and compare strings in JSP, use JSTL functions as shown below

<c:set var="mystring" value="<%=student_code%>"/>

<c:if test="${fn:substring(mystring, 0, 3) == 'MLV'}">
     <%count1++;%>
<c:if>

要使上述JSTL代码正常工作,您还需要在JSP中的以下标记库中导入

Also for above JSTL code to work you would need to import below taglibs in JSP

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

这篇关于如何在Jsp中比较两个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆