countmatchestag.java

来自「jakarta-taglibs」· Java 代码 · 共 73 行

JAVA
73
字号
/*
 * Copyright 1999,2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.taglibs.string;

import javax.servlet.jsp.JspException;
import org.apache.commons.lang.StringUtils;

/**
 * Return the number of times that the body String 
 * contains the sub-string.
 *
 * <dl>
 * <dt>substring</dt><dd>
 *             Substring to count number of.
 *             Required.
 * </dd>
 * </dl>
 * 
 * @author bayard@generationjava.com
 */
public class CountMatchesTag extends StringTagSupport {

    private String substring;

    public CountMatchesTag() {
        super();
    }

    /**
     * Get the substring property
     *
     * @return String property
     */
    public String getSubstring() {
        return this.substring;
    }

    /**
     * Set the substring property
     *
     * @param substring String property
     */
    public void setSubstring(String substring) {
        this.substring = substring;
    }



    public String changeString(String text) throws JspException {
        return ""+StringUtils.countMatches(text, substring);
    }

    public void initAttributes() {

        this.substring = null;

    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?