Java通用字符处理类

24 篇文章 0 订阅

Java 通用字符处理类

代码实例:

[java]  view plain copy
  1. package cn.edu.hbcit.ips.util;  
  2. public class CommenBean {  
  3.   /******************************************** 
  4.    public synchronized String HTMLcode(String TXTcode)   功能:文本替换 
  5.    public synchronized String Unhtmlcode(String str)   功能:(不完全)反文本替换 
  6.    public synchronized String Unhtmlcodea(String str)   功能:反文本替换 
  7.    public synchronized boolean emailcheck (String email)   功能:检查一个字符串是否符合E-Mail 
  8.    public synchronized boolean isemailstr(String email)   功能:检查一个字符串是否符合E-Mail 
  9.    public synchronized boolean isqqstr(String qq)    功能:检查一个字符串是否符合QQ 
  10.    public synchronized boolean isnumstr(String num)   功能:检查一个字符串是否为一数字串 
  11.    public synchronized String userstrlow(String user)   功能:替换用户名中不合法的部分 
  12.    public synchronized boolean userstrchk(String user)   功能:检查字符串是否符合用户名法则 
  13.    public synchronized boolean istelstr(String tel)   功能:检查字符串是否为TEL 
  14.    public synchronized boolean urlcheck(String url)   功能:检查字符串是否为URL 
  15.    public synchronized String isotogbk(String iso)    功能:ISO9006-1码转换为GBK 
  16.    public synchronized String gbktoiso(String gbk)    功能:GBK码转换为ISO9006-1 
  17.    public synchronized String dostrcut(String oldstr,int length)  功能:按汉字长换行(英文按半个字长) 
  18.    public synchronized String inttodateshow(int datenum)   功能:将1900年至时间的秒数换为日期字符串 
  19.    public synchronized String nowdateshow()    功能:显示当前日期 
  20.    public synchronized java.util.Date inttodate(int datenum)  功能:将秒数转换为日期 
  21.    public synchronized int datetoint()     功能:将时间换为从1900年至今的秒数 
  22.    public synchronized int datetoint(java.util.Date d)   功能:将时间换为从1900年至时间的秒数 
  23.    public synchronized String overlengthcut(String str,int length)  功能:截取前几个字符,单位为汉字字长 
  24.    public synchronized String replace(String str,String suba,String subb) 功能:字符串替换 
  25.    *********************************************/  
  26.   private static final String isostr = "ISO8859-1";  
  27.   private static final String gbkstr = "GBK";  
  28.   public CommenBean() {  
  29.   }  
  30.   public synchronized boolean emailcheck(String email) {  
  31.     if (email == null) {  
  32.       return false;  
  33.     }  
  34.     if (email.length() < 6) {  
  35.       return false;  
  36.     }  
  37.     if (email.indexOf("@") < 2) {  
  38.       return false;  
  39.     }  
  40.     if (email.indexOf(".") < 4) {  
  41.       return false;  
  42.     }  
  43.     if (email.endsWith(".") || email.endsWith("@")) {  
  44.       return false;  
  45.     }  
  46.     if (email.lastIndexOf("@") > email.lastIndexOf(".") - 1) {  
  47.       return false;  
  48.     }  
  49.     if (email.lastIndexOf("@") != email.indexOf("@")) {  
  50.       return false;  
  51.     }  
  52.     String[] lowstr = {  
  53.                       "/'""/"", "/n", "&", "/t", "/r", "<", ">", "/", "//",  
  54.                       "#"};  
  55.     for (int i = 0; i < lowstr.length; i++) {  
  56.       if (email.indexOf("lowstr") > 0) {  
  57.         return false;  
  58.       }  
  59.     }  
  60.     return true;  
  61.   }  
  62.   public synchronized boolean isemailstr(String email) {  
  63.     if (email == null) {  
  64.       return false;  
  65.     }  
  66.     if (email.indexOf("@") == -1 || email.indexOf(".") == -1 ||  
  67.         email.length() < 6) {  
  68.       return false;  
  69.     }  
  70.     return true;  
  71.   }  
  72.   public synchronized boolean isqqstr(String qq) {  
  73.     if (qq == null) {  
  74.       return false;  
  75.     }  
  76.     if (qq.length() > 12) {  
  77.       return false;  
  78.     }  
  79.     if (qq.length() < 5) {  
  80.       return false;  
  81.     }  
  82.     for (int i = 0; i < qq.length(); i++) {  
  83.       if (!(((int) qq.charAt(i)) <= 57 && ((int) qq.charAt(i)) >= 48)) {  
  84.         return false;  
  85.       }  
  86.     }  
  87.     return true;  
  88.   }  
  89.   public synchronized boolean isnumstr(String num) {  
  90.     if (num == null) {  
  91.       return false;  
  92.     }  
  93.     if (num.length() < 1) {  
  94.       return false;  
  95.     }  
  96.     for (int i = 0; i < num.length(); i++) {  
  97.       if (!(((int) num.charAt(i)) <= 57 && ((int) num.charAt(i)) >= 48)) {  
  98.         return false;  
  99.       }  
  100.     }  
  101.     return true;  
  102.   }  
  103.   public synchronized String userstrlow(String user) {  
  104.     String newuserstr = user.trim();  
  105.     char[] lowstr = {  
  106.                     '/'', '/"', '/n', '&', '/t', '/r', '<', '>', '/', '//', '#'};  
  107.     for (int i = 0; i < lowstr.length; i++) {  
  108.       newuserstr = newuserstr.replace(lowstr[i], '+');  
  109.     }  
  110.     return newuserstr;  
  111.   }  
  112.   public synchronized boolean userstrchk(String user) {  
  113.     String newuserstr = user.trim();  
  114.     char[] lowstr = {  
  115.                     '/'', '/"', '/n', '&', '/t', '/r', '<', '>', '/', '//', '#',  
  116.                     '~''`''!''@''$''%''^''*''('')''-''_',  
  117.                     '+''=''|''?'','';''.'};  
  118.     for (int i = 0; i < lowstr.length; i++) {  
  119.       newuserstr = newuserstr.replace(lowstr[i], '+');  
  120.     }  
  121.     return (user.equals(newuserstr)) ? true : false;  
  122.   }  
  123.   public synchronized boolean istelstr(String tel) {  
  124.     if (tel == null) {  
  125.       return false;  
  126.     }  
  127.     if (tel.length() < 1) {  
  128.       return false;  
  129.     }  
  130.     if (tel.length() > 32) {  
  131.       return false;  
  132.     }  
  133.     for (int i = 0; i < tel.length(); i++) {  
  134.       if (!(((int) tel.charAt(i)) <= 57 && ((int) tel.charAt(i)) >= 48)) {  
  135.         if (tel.charAt(i) != '-') {  
  136.           return false;  
  137.         }  
  138.       }  
  139.     }  
  140.     return true;  
  141.   }  
  142.   public synchronized boolean urlcheck(String url) {  
  143.     if (url == null) {  
  144.       return false;  
  145.     }  
  146.     if (url.length() < 10) {  
  147.       return false;  
  148.     }  
  149.     String urls = url.toLowerCase();  
  150.     if (!urls.startsWith("http://")) {  
  151.       return false;  
  152.     }  
  153.     if (url.indexOf("<") > 0 || url.indexOf(">") > 0) {  
  154.       return false;  
  155.     }  
  156.     return true;  
  157.   }  
  158.   public synchronized String isotogbk(String iso) throws Exception {  
  159.     if (iso != null) {  
  160.       return (new String(iso.getBytes(isostr), gbkstr));  
  161.     }  
  162.     if (iso.length() < 1) {  
  163.       return "";  
  164.     }  
  165.     return null;  
  166.   }  
  167.   public synchronized String gbktoiso(String gbk) throws Exception {  
  168.     if (gbk != null) {  
  169.       return (new String(gbk.getBytes(gbkstr), isostr));  
  170.     }  
  171.     if (gbk.length() < 1) {  
  172.       return "";  
  173.     }  
  174.     return null;  
  175.   }  
  176.   public synchronized String HTMLcode(String TXTcode) {  
  177.     String newstr = "";  
  178.     if (TXTcode == null) {  
  179.       return "";  
  180.     }  
  181.     newstr = TXTcode;  
  182.     newstr = replace(newstr, "&""&");  
  183.     newstr = replace(newstr, "/"", """);  
  184.     newstr = replace(newstr, " "" ");  
  185.     newstr = replace(newstr, "<""<");  
  186.     newstr = replace(newstr, ">"">");  
  187.     newstr = replace(newstr, "/'""'");  
  188.     return newstr;  
  189.   }  
  190.   public synchronized String Unhtmlcode(String str) {  
  191.     String newstr = "";  
  192.     if (str == null) {  
  193.       return "";  
  194.     }  
  195.     if (str.length() < 1) {  
  196.       return "";  
  197.     }  
  198.     newstr = str;  
  199.     newstr = replace(newstr, "&""&");  
  200.     //newstr=replace(newstr,""","/"");  
  201.     newstr = replace(newstr, " "" ");  
  202.     newstr = replace(newstr, """, "/"");  
  203.     //newstr=replace(newstr,"<","<");  
  204.     //newstr=replace(newstr,">",">");  
  205.     newstr = replace(newstr, "'""/'");  
  206.     return newstr;  
  207.   }  
  208.   public synchronized String Unhtmlcodea(String str) {  
  209.     String newstr = "";  
  210.     if (str == null) {  
  211.       return "";  
  212.     }  
  213.     if (str.length() < 1) {  
  214.       return "";  
  215.     }  
  216.     newstr = str;  
  217.     newstr = replace(newstr, "&""&");  
  218.     newstr = replace(newstr, """, "/"");  
  219.     newstr = replace(newstr, " "" ");  
  220.     newstr = replace(newstr, "<""<");  
  221.     newstr = replace(newstr, ">"">");  
  222.     newstr = replace(newstr, "'""/'");  
  223.     return newstr;  
  224.   }  
  225.   public synchronized String dostrcut(String oldstr, int length) {  
  226.     int i = 0;  
  227.     int j = 0;  
  228.     int k = 0;  
  229.     String newstr = "";  
  230.     if (oldstr == null) {  
  231.       return "";  
  232.     }  
  233.     if (length <= 0) {  
  234.       return "";  
  235.     }  
  236.     for (i = 0; i < oldstr.length(); i++) {  
  237.       if (oldstr.charAt(i) == '/n') {  
  238.         j = 0;  
  239.       }  
  240.       else if (((int) (oldstr.charAt(i))) > 255) {  
  241.         j += 2;  
  242.       }  
  243.       else {  
  244.         j++;  
  245.       }  
  246.       if ((j / 2) >= length) {  
  247.         newstr = newstr.concat(oldstr.substring(k, i) + "/n");  
  248.         k = i;j = 0;  
  249.      }  
  250.     }  
  251.     newstr = newstr.concat(oldstr.substring(k) + "/n");  
  252.     return newstr;  
  253.   }  
  254.   public synchronized String inttodateshow(int datenum) {  
  255.     int year = 0;  
  256.     int month = 0;  
  257.     int day = 0;  
  258.     int hour = 0;  
  259.     int minute = 0;  
  260.     int second = 0;  
  261.     String datestr = "";  
  262.     java.util.Date d;  
  263.     d = new java.util.Date((long) (datenum) * 1000);  
  264.     java.util.Calendar ds = java.util.Calendar.getInstance();  
  265.     ds.setTime(d);  
  266.     year = ds.get(java.util.Calendar.YEAR);  
  267.     month = ds.get(java.util.Calendar.MONTH);  
  268.     day = ds.get(java.util.Calendar.DATE);  
  269.     hour = ds.get(java.util.Calendar.HOUR_OF_DAY);  
  270.     minute = ds.get(java.util.Calendar.MINUTE);  
  271.     second = ds.get(java.util.Calendar.SECOND);  
  272.     datestr = Integer.toString(year) + "/" + Integer.toString(1 + month) + "/" +  
  273.               Integer.toString(day);  
  274.     return datestr;  
  275.   }  
  276.   public synchronized String nowdateshow() {  
  277.     int year = 0;  
  278.     int month = 0;  
  279.     int day = 0;  
  280.     int hour = 0;  
  281.     int minute = 0;  
  282.     int second = 0;  
  283.     String datestr = "";  
  284.     java.util.Calendar ds = java.util.Calendar.getInstance();  
  285.     year = ds.get(java.util.Calendar.YEAR);  
  286.     month = ds.get(java.util.Calendar.MONTH);  
  287.     day = ds.get(java.util.Calendar.DATE);  
  288.     hour = ds.get(java.util.Calendar.HOUR_OF_DAY);  
  289.     minute = ds.get(java.util.Calendar.MINUTE);  
  290.     second = ds.get(java.util.Calendar.SECOND);  
  291.     datestr = Integer.toString(year) + "/" + Integer.toString(1 + month) + "/" +  
  292.               Integer.toString(day);  
  293.     return datestr;  
  294.   }  
  295.   public synchronized java.util.Date inttodate(int datenum) {  
  296.     int year = 0;  
  297.     int month = 0;  
  298.     int day = 0;  
  299.     String datestr = "";  
  300.     java.util.Date d;  
  301.     d = new java.util.Date((long) (datenum) * 1000);  
  302.     return d;  
  303.   }  
  304.   public synchronized int datetoint() {  
  305.     java.util.Date d = null;  
  306.     long ds = 0;  
  307.     d = new java.util.Date();  
  308.     ds = d.getTime();  
  309.     return (int) (ds / 1000);  
  310.   }  
  311.   public synchronized int datetoint(java.util.Date d) {  
  312.     long ds = 0;  
  313.     ds = d.getTime();  
  314.     return (int) (ds / 1000);  
  315.   }  
  316.   public synchronized String overlengthcut(String str, int length) {  
  317.     int i = 0;  
  318.     int j = 0;  
  319.     if (str == null) {  
  320.       return "";  
  321.     }  
  322.     if (length < 0) {  
  323.       return "";  
  324.     }  
  325.     if (str.length() <= length) {  
  326.       return str;  
  327.     }  
  328.     for (i = 0; i < str.length(); i++) {  
  329.       if (((int) (str.charAt(i))) > 255) {  
  330.         j += 2;  
  331.       }  
  332.       else {  
  333.         j++;  
  334.       }  
  335.       if ((j / 2) >= length) {  
  336.         return str.substring(0, i);  
  337.       }  
  338.     }  
  339.     return str;  
  340.   }  
  341.   public synchronized String replace(String str, String suba, String subb) {  
  342.     String newstr = "";  
  343.     int start = 0;  
  344.     int offset = 0;  
  345.     int subalength = 0;  
  346.     int strlength = 0;  
  347.     if (str == null || suba == null || subb == null) {  
  348.       return str;  
  349.     }  
  350.     if (suba.equals(subb)) {  
  351.       return str;  
  352.     }  
  353.     if (str.length() < suba.length() || str.length() < subb.length()) {  
  354.       return str;  
  355.     }  
  356.     if (str.length() > 0 && suba.length() > 0 && subb.length() > 0) {  
  357.       subalength = suba.length();  
  358.       strlength = str.length();  
  359.       while (true) {  
  360.         if (str.indexOf(suba) < 0) {  
  361.           break;  
  362.         }  
  363.         if (offset > strlength) {  
  364.           break;  
  365.         }  
  366.         start = str.indexOf(suba, offset);  
  367.         if (start < offset) {  
  368.           break;  
  369.         }  
  370.         newstr = newstr.concat(str.substring(offset, start));  
  371.         newstr = newstr.concat(subb);  
  372.         offset = start + subalength;  
  373.       }  
  374.       newstr = newstr.concat(str.substring(offset));  
  375.       return newstr;  
  376.     }  
  377.     else {  
  378.       return str;  
  379.     }  
  380.   }  
  381. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值