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
*
* 一个带输入框的窗体,用户在此输入框内输入许可证密钥。您希望允许输入1110-JAVA格式的密钥。您必须进行以下操作:
*
* 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.length() != 4 || keySplit.length() != 4) {
return false;
}
//Check if first part is numeric
if (!StringUtils.isNumeric(keySplit)) {
return false;
}
//Check if second part contains only
//the four characters 'J', 'A', 'V' and 'A'
if (! StringUtils.containsOnly(keySplit,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') != 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
页:
[1]