JSTL或JSP中的lastIndexOf

时间:2013-09-04 03:47:45

标签: jsp jstl

我可以在JSTL或JSP中找出lastIndexOf()函数吗? 我刚刚在JSTL中找到了方法
int indexOf(java.lang.String, java.lang.String)。或者还有工作吗?

3 个答案:

答案 0 :(得分:1)

如何使用除最后一个组件之外的所有组件的fn:split和总和(fn:length)。
但是在后端执行所有逻辑要好得多,只需在对象中添加其他属性即可。

答案 1 :(得分:1)

这是一个似乎对我有用的。将myStr和lastIndexOf修改为您想要的内容。

    <!-- Prepare variables -->
    <c:set var="myStr" value="this_is my. string_ok"/>
    <c:set var="lastIndexOf" value="_"/>
    <c:set var="lastIndexFoundAt" value=""/>
    <c:set var="myStrSize" value="${fn:length(myStr) }"/>
    <c:set var="indexIn" value="0"/>
    <c:set var="indexOut" value="1"/>

    <!-- iterate through string -->
    <c:forEach var="i" begin="0" end="${myStrSize}">

            <c:if test="${indexOut <= myStrSize }">
                <c:set var="char" value="${fn:substring(myStr, indexIn, indexOut) }"/>
                <c:if test="${char == lastIndexOf }">
                    <c:set var="lastIndexFoundAt" value="${i}"/>
                </c:if>

                <c:set var="indexIn" value="${indexIn + 1 }"/> <!-- increment -->
                <c:set var="indexOut" value="${indexOut + 1 }"/> <!-- increment -->
            </c:if>

    </c:forEach>

测试索引,使用

    <c:out value="${lastIndexFoundAt}/>

答案 2 :(得分:1)

可以尝试:

<c:set value="${fn:split('fooBar/BarFoo/Bar','/')}" var="separatorPosition" />
     <c:out value="${separatorPosition[fn:length(separatorPosition)-1]}">
   </c:out>