package cn.vetech.framework.util;
/**
* 字符串处理类
* @author vesoft
*
*/
import java.util.Random;
public class VeStr {
/**
* 判断一个字符串是否是整数
*
* @param validString
* @return
*/
public static boolean isInteger(String validString) {
if (validString == null)
return false;
byte[] tempbyte = validString.getBytes();
for (int i = 0; i < validString.length(); i++) {
// by=tempbyte[i];
if ((tempbyte[i] < 48) || (tempbyte[i] > 57)) {
return false;
}
}
return true;
}
/**
* 判断一个数是否是双精度型数
*
* @param validString
* @return
*/
public static boolean isDouble(String validString) {
if (validString == null)
return false;
int k = 0;
byte[] tempbyte = validString.getBytes();
for (int i = 0; i < validString.length(); i++) {
if (tempbyte[i] == 46) {
k++;
}
}
if (k > 1)
return false; // k>1表示有两个以上的小数点,为非法数字
for (int i = 0; i < validString.length(); i++) {
if (tempbyte[i] != 46) { // 46为"."的ASCII码
// ,48为"0"的ASCII码,57是"9"的ASCII码
if ((tempbyte[i] < 48) || (tempbyte[i] > 57)) {
return false;
}
}
}
return true;
}
/**
* 判断字符串是否只包括字母和数字
*
* @param validString
* @return
*/
public static boolean isChar(String validString) {
if (validString == null)
return false;
byte[] tempbyte = validString.getBytes();
for (int i = 0; i < validString.length(); i++) {
if ((tempbyte[i] < 48) || ((tempbyte[i] > 57) & (tempbyte[i] < 65)) || (tempbyte[i] > 122)
|| ((tempbyte[i] > 90) & (tempbyte[i] < 97))) {
return false;
}
}
return true;
}
/**
* 判断字符串是否只包含字母
*
* @param validString
* @return
*/
public static boolean isLetter(String validString) {
if (validString == null)
return false;
byte[] tempbyte = validString.getBytes();
for (int i = 0; i < validString.length(); i++) {
if ((tempbyte[i] < 65) || (tempbyte[i] > 122) || ((tempbyte[i] > 90) & (tempbyte[i] < 97))) {
return false;
}
}
return true;
}
/**
* 字符串替换
*
* @param source
* @param oldString
* @param newString
* @return
*/
public static String Replace(String source, String oldString, String newString) {
if (source == null)
return null;
StringBuffer output = new StringBuffer();
int lengOfsource = source.length();
int lengOfold = oldString.length();
int posStart = 0;
int pos;
while ((pos = source.indexOf(oldString, posStart)) >= 0) {
output.append(source.substring(posStart, pos));
output.append(newString);
posStart = pos + lengOfold;
}
if (posStart < lengOfsource) {
output.append(source.substring(posStart));
}
return output.toString();
}
/**
* 判断一个字符串是否包含另一个字符串
*
* @param source
* @param findString
* @return
*/
public static boolean isInclude(String source, String findString) {
if (source == null)
return false;
if (findString == null)
return false;
int posStart = 0;
int pos = source.indexOf(findString, posStart);
boolean jj = false;
if (pos >= 0) {
jj = true;
}
return jj;
}
/**
* 字符串转换到html语法
*
* @param s
* @return
*/
public static String toHtml(String s) {
s = Replace(s, "&", "&");
s = Replace(s, "<", "<");
s = Replace(s, ">", ">");
s = Replace(s, "/t", " ");
s = Replace(s, "/r/n", "/n");
// s = Replace(s,"/"","'");
s = Replace(s, "/n", "<br>");
s = Replace(s, " ", " ");
// s = Replace(s,"","'");
s = Replace(s, "'", "'");
s = Replace(s, "//", "\");
return s;
}
/**
* 相对于上一接口的反转换
*
* @param s
* @return
*/
public static String unHtml(String s) {
s = Replace(s, "<br>", "/n");
s = Replace(s, "<", "<");
s = Replace(s, " ", " ");
s = Replace(s, ">", ">");
return s;
}
/**
* 返回一个随机数
*
* @param i
* @return
*/
public static String getRandom(int i) {
Random jjj = new Random();
if (i == 0)
return "";
String jj = "";
for (int k = 0; k < i; k++) {
jj = jj + jjj.nextInt(9);
;
}
return jj;
}
/***************************************************************************
* 判断是否是时间
**************************************************************************/
public static boolean Isdate(String strdate) {
if (strdate == null || strdate.equals(""))
return false;
if (strdate.length() != 8 && strdate.length() != 10)
return false;
String strarray[];
int intyear = 1990, intmonth = 12, intday = 10;
strarray = strdate.split("-");
if (strdate.length() == 10) {
if (strarray.length != 3) {
strarray = strdate.split("/");
if (strarray.length != 3)
return false;
}
intyear = Integer.parseInt(strarray[0]);
intmonth = Integer.parseInt(strarray[1]);
intday = Integer.parseInt(strarray[2]);
}
if (strdate.length() == 8) {
intyear = Integer.parseInt(strdate.substring(0, 4));
intmonth = Integer.parseInt(strdate.substring(4, 6));
intday = Integer.parseInt(strdate.substring(6, 8));
}
if (intyear > 9999 || intyear < 1000) {
return false;
}
if (intmonth > 12 || intmonth < 1) {
return false;
}
if (((intmonth == 1) || (intmonth == 3) || (intmonth == 5) || (intmonth == 7) || (intmonth == 8)
|| (intmonth == 10) || (intmonth == 12))
&& ((intday > 31) || (intday < 1))) {
return false;
}
if ((intmonth == 4 || intmonth == 6 || intmonth == 9 || intmonth == 11) && (intday > 30 || intday < 1)) {
return false;
}
if (intmonth == 2) {
if (intday < 1) {
return false;
}
boolean boolLeapYear = false;
if ((intyear % 100) == 0) {
if ((intyear % 400) == 0)
boolLeapYear = true;
} else {
if ((intyear % 4) == 0)
boolLeapYear = true;
}
if (boolLeapYear) {
if (intday > 29) {
return false;
}
} else {
if (intday > 28) {
return false;
}
}
}
return true;
}
/**
* url下一个链接符号
*
* @param url
* @return
*/
public static String nextFgf(String url) {
if (url != null && !"".equals(url)) {
if (url.indexOf("?") != -1) {
return "&";
} else {
return "?";
}
}
return "";
}
}