org.apache.commons.lang.StringUtils 的应用

 

import org.apache.commons.lang.StringUtils;

/**
 * Created by IntelliJ IDEA.
 * User: lly
 * Date: 2006-11-16
 * Time: 11:01:36
 * <p/>
 *  一个带输入框的窗体,用户在此输入框内输入许可证密钥。您希望允许输入1110-JAVA格式的密钥。您必须进行以下操作:
 * <p/>
 * 1.检查是否为空字符串。
 * 2.忽略空格。
 * 3.密钥区分大小写。
 * 4.用“-”标记分隔密钥字符串,然后检查第一部分是否全部是数字,第二部分包含的字符是否只来自有效字符集“J”、“A”、“V”、“A”。
 * 5.两个部分均应有四个字符。
 * 6.第一部分的第四个数字应该是“0”。
 */
public class StringUtilsTest {
    /**
     * Check if the key is valid
     *
     * @param key license key value
     * @return true if key is valid, false otherwise.
     */
    public static boolean checkLicenseKey(String key) {
        //checks if empty or null
        if (StringUtils.isBlank(key)) {
            return false;
        }

        //delete all white space
        key = StringUtils.deleteWhitespace(key);

        //Split String using the - separator
        String[] keySplit = StringUtils.split(key, "-");

        //check lengths of whole and parts
        if (keySplit.length != 2 || keySplit[0].length() != 4 || keySplit[1].length() != 4) {
            return false;
        }

        //Check if first part is numeric
        if (!StringUtils.isNumeric(keySplit[0])) {
            return false;
        }

        //Check if second part contains only
        //the four characters 'J', 'A', 'V' and 'A'
        if (! StringUtils.containsOnly(keySplit[1],new char[]{'J', 'A', 'V', 'A'})) {
            return false;
        }

        //Check if the fourth character
        //in the first part is a '0'
        if (StringUtils.indexOf(keySplit[0], '0') != 3) {
            return false;
        }

        //If all conditions are fulfilled, key is valid.
        return true;
    }

    public static void main(String[] args) {
        String pass = "1110-JAVA";
        System.out.println("this.clone().equals()checkLicenseKey(pass) = " + StringUtilsTest.checkLicenseKey(pass));
    }
}

引用于:http://dev2dev.bea.com.cn/techdoc/2005071902.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值