Java 通用字符处理类
代码实例:
- package cn.edu.hbcit.ips.util;
- public class CommenBean {
- /********************************************
- public synchronized String HTMLcode(String TXTcode) 功能:文本替换
- public synchronized String Unhtmlcode(String str) 功能:(不完全)反文本替换
- public synchronized String Unhtmlcodea(String str) 功能:反文本替换
- public synchronized boolean emailcheck (String email) 功能:检查一个字符串是否符合E-Mail
- public synchronized boolean isemailstr(String email) 功能:检查一个字符串是否符合E-Mail
- public synchronized boolean isqqstr(String qq) 功能:检查一个字符串是否符合QQ
- public synchronized boolean isnumstr(String num) 功能:检查一个字符串是否为一数字串
- public synchronized String userstrlow(String user) 功能:替换用户名中不合法的部分
- public synchronized boolean userstrchk(String user) 功能:检查字符串是否符合用户名法则
- public synchronized boolean istelstr(String tel) 功能:检查字符串是否为TEL
- public synchronized boolean urlcheck(String url) 功能:检查字符串是否为URL
- public synchronized String isotogbk(String iso) 功能:ISO9006-1码转换为GBK
- public synchronized String gbktoiso(String gbk) 功能:GBK码转换为ISO9006-1
- public synchronized String dostrcut(String oldstr,int length) 功能:按汉字长换行(英文按半个字长)
- public synchronized String inttodateshow(int datenum) 功能:将1900年至时间的秒数换为日期字符串
- public synchronized String nowdateshow() 功能:显示当前日期
- public synchronized java.util.Date inttodate(int datenum) 功能:将秒数转换为日期
- public synchronized int datetoint() 功能:将时间换为从1900年至今的秒数
- public synchronized int datetoint(java.util.Date d) 功能:将时间换为从1900年至时间的秒数
- public synchronized String overlengthcut(String str,int length) 功能:截取前几个字符,单位为汉字字长
- public synchronized String replace(String str,String suba,String subb) 功能:字符串替换
- *********************************************/
- private static final String isostr = "ISO8859-1";
- private static final String gbkstr = "GBK";
- public CommenBean() {
- }
- public synchronized boolean emailcheck(String email) {
- if (email == null) {
- return false;
- }
- if (email.length() < 6) {
- return false;
- }
- if (email.indexOf("@") < 2) {
- return false;
- }
- if (email.indexOf(".") < 4) {
- return false;
- }
- if (email.endsWith(".") || email.endsWith("@")) {
- return false;
- }
- if (email.lastIndexOf("@") > email.lastIndexOf(".") - 1) {
- return false;
- }
- if (email.lastIndexOf("@") != email.indexOf("@")) {
- return false;
- }
- String[] lowstr = {
- "/'", "/"", "/n", "&", "/t", "/r", "<", ">", "/", "//",
- "#"};
- for (int i = 0; i < lowstr.length; i++) {
- if (email.indexOf("lowstr") > 0) {
- return false;
- }
- }
- return true;
- }
- public synchronized boolean isemailstr(String email) {
- if (email == null) {
- return false;
- }
- if (email.indexOf("@") == -1 || email.indexOf(".") == -1 ||
- email.length() < 6) {
- return false;
- }
- return true;
- }
- public synchronized boolean isqqstr(String qq) {
- if (qq == null) {
- return false;
- }
- if (qq.length() > 12) {
- return false;
- }
- if (qq.length() < 5) {
- return false;
- }
- for (int i = 0; i < qq.length(); i++) {
- if (!(((int) qq.charAt(i)) <= 57 && ((int) qq.charAt(i)) >= 48)) {
- return false;
- }
- }
- return true;
- }
- public synchronized boolean isnumstr(String num) {
- if (num == null) {
- return false;
- }
- if (num.length() < 1) {
- return false;
- }
- for (int i = 0; i < num.length(); i++) {
- if (!(((int) num.charAt(i)) <= 57 && ((int) num.charAt(i)) >= 48)) {
- return false;
- }
- }
- return true;
- }
- public synchronized String userstrlow(String user) {
- String newuserstr = user.trim();
- char[] lowstr = {
- '/'', '/"', '/n', '&', '/t', '/r', '<', '>', '/', '//', '#'};
- for (int i = 0; i < lowstr.length; i++) {
- newuserstr = newuserstr.replace(lowstr[i], '+');
- }
- return newuserstr;
- }
- public synchronized boolean userstrchk(String user) {
- String newuserstr = user.trim();
- char[] lowstr = {
- '/'', '/"', '/n', '&', '/t', '/r', '<', '>', '/', '//', '#',
- '~', '`', '!', '@', '$', '%', '^', '*', '(', ')', '-', '_',
- '+', '=', '|', '?', ',', ';', '.'};
- for (int i = 0; i < lowstr.length; i++) {
- newuserstr = newuserstr.replace(lowstr[i], '+');
- }
- return (user.equals(newuserstr)) ? true : false;
- }
- public synchronized boolean istelstr(String tel) {
- if (tel == null) {
- return false;
- }
- if (tel.length() < 1) {
- return false;
- }
- if (tel.length() > 32) {
- return false;
- }
- for (int i = 0; i < tel.length(); i++) {
- if (!(((int) tel.charAt(i)) <= 57 && ((int) tel.charAt(i)) >= 48)) {
- if (tel.charAt(i) != '-') {
- return false;
- }
- }
- }
- return true;
- }
- public synchronized boolean urlcheck(String url) {
- if (url == null) {
- return false;
- }
- if (url.length() < 10) {
- return false;
- }
- String urls = url.toLowerCase();
- if (!urls.startsWith("http://")) {
- return false;
- }
- if (url.indexOf("<") > 0 || url.indexOf(">") > 0) {
- return false;
- }
- return true;
- }
- public synchronized String isotogbk(String iso) throws Exception {
- if (iso != null) {
- return (new String(iso.getBytes(isostr), gbkstr));
- }
- if (iso.length() < 1) {
- return "";
- }
- return null;
- }
- public synchronized String gbktoiso(String gbk) throws Exception {
- if (gbk != null) {
- return (new String(gbk.getBytes(gbkstr), isostr));
- }
- if (gbk.length() < 1) {
- return "";
- }
- return null;
- }
- public synchronized String HTMLcode(String TXTcode) {
- String newstr = "";
- if (TXTcode == null) {
- return "";
- }
- newstr = TXTcode;
- newstr = replace(newstr, "&", "&");
- newstr = replace(newstr, "/"", """);
- newstr = replace(newstr, " ", " ");
- newstr = replace(newstr, "<", "<");
- newstr = replace(newstr, ">", ">");
- newstr = replace(newstr, "/'", "'");
- return newstr;
- }
- public synchronized String Unhtmlcode(String str) {
- String newstr = "";
- if (str == null) {
- return "";
- }
- if (str.length() < 1) {
- return "";
- }
- newstr = str;
- newstr = replace(newstr, "&", "&");
- //newstr=replace(newstr,""","/"");
- newstr = replace(newstr, " ", " ");
- newstr = replace(newstr, """, "/"");
- //newstr=replace(newstr,"<","<");
- //newstr=replace(newstr,">",">");
- newstr = replace(newstr, "'", "/'");
- return newstr;
- }
- public synchronized String Unhtmlcodea(String str) {
- String newstr = "";
- if (str == null) {
- return "";
- }
- if (str.length() < 1) {
- return "";
- }
- newstr = str;
- newstr = replace(newstr, "&", "&");
- newstr = replace(newstr, """, "/"");
- newstr = replace(newstr, " ", " ");
- newstr = replace(newstr, "<", "<");
- newstr = replace(newstr, ">", ">");
- newstr = replace(newstr, "'", "/'");
- return newstr;
- }
- public synchronized String dostrcut(String oldstr, int length) {
- int i = 0;
- int j = 0;
- int k = 0;
- String newstr = "";
- if (oldstr == null) {
- return "";
- }
- if (length <= 0) {
- return "";
- }
- for (i = 0; i < oldstr.length(); i++) {
- if (oldstr.charAt(i) == '/n') {
- j = 0;
- }
- else if (((int) (oldstr.charAt(i))) > 255) {
- j += 2;
- }
- else {
- j++;
- }
- if ((j / 2) >= length) {
- newstr = newstr.concat(oldstr.substring(k, i) + "/n");
- k = i;j = 0;
- }
- }
- newstr = newstr.concat(oldstr.substring(k) + "/n");
- return newstr;
- }
- public synchronized String inttodateshow(int datenum) {
- int year = 0;
- int month = 0;
- int day = 0;
- int hour = 0;
- int minute = 0;
- int second = 0;
- String datestr = "";
- java.util.Date d;
- d = new java.util.Date((long) (datenum) * 1000);
- java.util.Calendar ds = java.util.Calendar.getInstance();
- ds.setTime(d);
- year = ds.get(java.util.Calendar.YEAR);
- month = ds.get(java.util.Calendar.MONTH);
- day = ds.get(java.util.Calendar.DATE);
- hour = ds.get(java.util.Calendar.HOUR_OF_DAY);
- minute = ds.get(java.util.Calendar.MINUTE);
- second = ds.get(java.util.Calendar.SECOND);
- datestr = Integer.toString(year) + "/" + Integer.toString(1 + month) + "/" +
- Integer.toString(day);
- return datestr;
- }
- public synchronized String nowdateshow() {
- int year = 0;
- int month = 0;
- int day = 0;
- int hour = 0;
- int minute = 0;
- int second = 0;
- String datestr = "";
- java.util.Calendar ds = java.util.Calendar.getInstance();
- year = ds.get(java.util.Calendar.YEAR);
- month = ds.get(java.util.Calendar.MONTH);
- day = ds.get(java.util.Calendar.DATE);
- hour = ds.get(java.util.Calendar.HOUR_OF_DAY);
- minute = ds.get(java.util.Calendar.MINUTE);
- second = ds.get(java.util.Calendar.SECOND);
- datestr = Integer.toString(year) + "/" + Integer.toString(1 + month) + "/" +
- Integer.toString(day);
- return datestr;
- }
- public synchronized java.util.Date inttodate(int datenum) {
- int year = 0;
- int month = 0;
- int day = 0;
- String datestr = "";
- java.util.Date d;
- d = new java.util.Date((long) (datenum) * 1000);
- return d;
- }
- public synchronized int datetoint() {
- java.util.Date d = null;
- long ds = 0;
- d = new java.util.Date();
- ds = d.getTime();
- return (int) (ds / 1000);
- }
- public synchronized int datetoint(java.util.Date d) {
- long ds = 0;
- ds = d.getTime();
- return (int) (ds / 1000);
- }
- public synchronized String overlengthcut(String str, int length) {
- int i = 0;
- int j = 0;
- if (str == null) {
- return "";
- }
- if (length < 0) {
- return "";
- }
- if (str.length() <= length) {
- return str;
- }
- for (i = 0; i < str.length(); i++) {
- if (((int) (str.charAt(i))) > 255) {
- j += 2;
- }
- else {
- j++;
- }
- if ((j / 2) >= length) {
- return str.substring(0, i);
- }
- }
- return str;
- }
- public synchronized String replace(String str, String suba, String subb) {
- String newstr = "";
- int start = 0;
- int offset = 0;
- int subalength = 0;
- int strlength = 0;
- if (str == null || suba == null || subb == null) {
- return str;
- }
- if (suba.equals(subb)) {
- return str;
- }
- if (str.length() < suba.length() || str.length() < subb.length()) {
- return str;
- }
- if (str.length() > 0 && suba.length() > 0 && subb.length() > 0) {
- subalength = suba.length();
- strlength = str.length();
- while (true) {
- if (str.indexOf(suba) < 0) {
- break;
- }
- if (offset > strlength) {
- break;
- }
- start = str.indexOf(suba, offset);
- if (start < offset) {
- break;
- }
- newstr = newstr.concat(str.substring(offset, start));
- newstr = newstr.concat(subb);
- offset = start + subalength;
- }
- newstr = newstr.concat(str.substring(offset));
- return newstr;
- }
- else {
- return str;
- }
- }
- }