Sunspl

          Hello,everyone,i am sun. 天道酬勤,笨鳥先飛.
          隨筆 - 47, 文章 - 0, 評論 - 24, 引用 - 0
          數(shù)據(jù)加載中……

          一些常用的方法

          //字符串初始化
          public String toString(String str){
           if(str == null){
            str = "";
           }
           if(str.equals("null")){
            str = "";
           }
           str = str.trim();
           return str;
          }
          //編碼轉(zhuǎn)換
          public String toGBK(String str){
           try{
            if(str == null){
             str = "";
            }else{
             str = new String(str.getBytes("ISO-8859-1"),"GBK");
            }
           }catch(Exception e){
            System.out.println("DealString::toGBK(String)運行時出錯:"+e;
           }
          }
          //UTF轉(zhuǎn)換
          public String to Utf8String(String src){
           byte[] b = src.getBytes();
           char[] c = new char[b.length];
           for(int i = 0; i < b.length; i++){
            c[i] = (char)(b[i]&0x00FF);
           }
           return new String(c);
          }
          //字符分隔
          public String[] splitStr(String str,char c){
           str += c;
           int n = 0;
           for(int i = 0; i < str.length();i++){
            if(str.charAt(i) == c){
             n ++;
            }
           }
           String out[] = new String[n];
           for(int i = 0; i < n; i++){
            int index = str.indexOf(c);
            out[i] = str.substring(0,index);
            str = str.substring(index+1),str.length());
           }
           return out;
          }
          //取得系統(tǒng)時間
          public String getDateTime(){
           java.text.SimpleDateFormat f = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           String time = f.format(new java.util.Date());
           return time;
          }
          //字符串替換
          public String Replace(String source,String oldString,String newString){
           StringBuffer output = new StringBuffer();
           int lengthOfSource = source.length();
           int lengthOfOld = 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 + lengthOfOld;
           }
           if(posStart < lengthOfSource){
            output.append(source.substring(posStart));
           }
           return output.toString();
          }

          //日期之差
          public long getDaysInterval(Date d1,Date d2){
           return (d2.getTime() - d1.getTime())/86400000;
          }
          //字符串右格式化
          public String toLengthStr(String instr,int len){
           int n = instr.length();
           for(int i = 0; i < (len - n); i++){
            instr = " " + instr;
           }
           return instr;
          }
          //字符串左格式化
          public String toLengthStr(String instr,int len){
           int n = instr.length();
           for(int i = 0; i < (len - n); i++){
            instr = instr + " ";
           }
           return instr;
          }
          //字符串位長度
          public int byteLength(String str){
           return ((str.getBytes()).length);
          }
          //字符串位截取
          public String strByteCopy(String str,int nEnd){
           if(nEnd == 0){
            return "";
           }
           byte[] byteStr = str.getBytes();
           int k = byteStr.length;
           String strSub = new String(byteStr,0,End<k?nEnd:k)
           if(strSub.length() == 0){
            strSub = new String(byteStr,0,nEnd-1);
           }
           return strSub;
          }

          posted on 2007-04-03 11:39 JavaSuns 閱讀(1089) 評論(1)  編輯  收藏

          評論

          # re: 一些常用的方法  回復(fù)  更多評論   

          不錯,都是開發(fā)中常用的方法
          2007-04-03 16:16 | yuri

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 黔东| 义马市| 贵定县| 宜兰县| 罗山县| 禹城市| 江华| 广丰县| 普安县| 屏南县| 红安县| 北安市| 鸡西市| 马鞍山市| 夏邑县| 石首市| 瑞金市| 杨浦区| 桐乡市| 河北省| 会泽县| 惠东县| 泗阳县| 永仁县| 鄄城县| 龙胜| 天门市| 新河县| 安溪县| 丹棱县| 晋城| 仙游县| 宾阳县| 杂多县| 凤城市| 霍邱县| 虎林市| 蓬莱市| 孟津县| 获嘉县| 化州市|