java对String的操作

摘要

java对String的操作,String是非常重要的一种数据结构。

package cn.ccb.odsbsx.common.util;
import java.util.*;
import java.text.*;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.*;
import java.net.MalformedURLException;
import java.net.URL;
import sun.io.*;
public class DealString {
    /**
     * 把null转化为""
     *
     * @param str
     * @return
     */
    public static String toString(String str) {
        if (str == null)
            str = "";
        if (str.equals("null"))
            str = "";
        str = str.trim();
        return str;
    }
    /**
     * 取标题的长度
     *
     * @param title
     * @param length
     * @return
     */
    public static String subTitle(String title, int length) {
        String fmts = null;
        if (length == 0 && length < 0) {
            return "";
        } else {
            if (title.length() > length) {
                fmts = title.substring(0, length - 1) + "...";
            } else {
                fmts = title;
            }
            return fmts;
        }
    }
    /**
     * 删除物理文件操作,一般获得的是tomacate的路径
     *
     * @param fileName
     */
    public static void delFile(String fileName) {
        String path = System.getProperty("user.dir");// 获得tomcate的相对路径
        String newFileName = path + "\\WebRoot\\" + fileName;
        File temp = new File(newFileName);
        temp.deleteOnExit();
    }
    /**
     * 重名文件
     *
     * @param fileName
     */
    public static void FileRenamer(String fileName) {
        try {
            String path = System.getProperty("user.dir");// 获得tomcate的相对路径
            String newFileName = path + "\\WebRoot\\" + fileName;
            File temp = new File(newFileName);
            String fileNameone = IdexOfString(fileName, "\\");
            String newFileNameone = getID() + ".xls";// 新文件的名字
            System.out.println("aa:" + newFileNameone);
            File newtemp = new File(newFileNameone);
            temp.renameTo(newtemp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 根据字符截取字符串长度
     *
     * @param str
     * @param str1
     * @return
     */
    public static String IdexOfString(String str, String str1) {
        if (str.lastIndexOf(str1) != -1) {
            int i = str.lastIndexOf(str1);
            str = str.substring(i + 1);
        }
        return str;
    }
    /**
     * 取得字符串从头开始指定长度子串
     *
     * @param str
     * @param nEnd
     * @return
     */
    public static String strByteCopy(String str, int nEnd) {
        if (nEnd == 0)
            return "";
        byte[] byteStr = str.getBytes();
        int k = byteStr.length;
        String strSub = new String(byteStr, 0, nEnd < k ? nEnd : k);
        if (strSub.length() == 0)
            strSub = new String(byteStr, 0, nEnd - 1);
        return strSub;
    }
    /**
     * 转换编码
     *
     * @param str
     * @return
     */
    public static 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);
        }
        return str;
    }
    /**
     * 字节大小转换
     *
     * @param size
     * @return
     */
    public static String toMKByte(int size) {
        if (size > (1024 * 1024)) {
            return ((float) size / (1024 * 1024) + "").substring(0, 4) + "MB";
        } else if (size > 1024) {
            return ((float) size / 1024 + "").substring(0, 4) + "KB";
        } else
            return size + "B";
    }
    /**
     * 取得此格式下的系统时间
     *
     * @return
     */
    public static String getID() {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyMMddHHmmss");
        long t = Long.parseLong(formatter.format(date));
        return formatter.format(date);
    }
    /** 取得系统时间 */
    public static 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 static long getDaysInterval(Date d1, Date d2) {
        return (d2.getTime() - d1.getTime()) / 86400000;
    }
    /**
     * 取得两个日期的天数之差
     *
     * @throws ParseException
     */
    public static String getDaysIntervalS(String d1, String d2) {
        String strdate = "";
        try {
            SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
            Date d3 = sdf.parse(d1);
            Date d4 = sdf.parse(d2);
            strdate = Long.toString((d4.getTime() - d3.getTime()) / 86400000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return strdate;
    }
    /**
     * 取得两个日期的天数之差 (当前日期和招标公告的截止日期相比)
     *
     * @param d1
     * @return
     */
    public static String getDaysIntervalNow(String d1) {
        String strdate = "";
        try {
            SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
            Date d4 = sdf.parse(d1);
            Date d3 = new Date();
            if (d4.compareTo(d3) < 0) {
                d4.getDay();
            }
            strdate = Long.toString((d4.getTime() - d3.getTime()) / 86400000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return strdate;
    }
    /**
     * 取得两个日期的天数之差 (当前日期和新闻公告的发布时间相比)
     *
     * @param d1
     * @return
     */
    public static int getDaysIntervalBefor(String d1) {
        int strdate = 0;
        try {
            SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
            Date d4 = sdf.parse(d1);
            Date d3 = new Date();
            strdate = (int) ((d3.getTime() - d4.getTime()) / 86400000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return strdate;
    }
    /**
     * 截取字符串放到一维数组中
     *
     * @param str
     * @param len
     * @return
     */
    public static String[] getCharArray(String str, int len) {
        String strArray[] = null;
        if (null != str && !"".equals(str)) {
            int strLen = str.length();
            int aa = 0;
            if (strLen % len == 0) {
                aa = strLen / len;
            } else {
                aa = strLen / len + 1;
            }
            strArray = new String[aa];// 定义已维数组
            // 将截取的字符串放到数组中
            int j = 0;
            StringBuffer buffer = new StringBuffer();
            for (int i = 1; i <= str.length(); i++) {
                if (i % len == 0) {
                    buffer.append(str.charAt(i - 1));
                    strArray[j] = buffer.toString();
                    j++;
                    buffer.delete(0, len);
                } else {
                    buffer.append(str.charAt(i - 1));
                }
            }
            if (strLen % len != 0) {
                strArray[j] = buffer.toString();
            }
        }
        return strArray;
    }
    public List getQcList(List leftList, List rightList) {
        List arrayListthree = new ArrayList();
        int j = 0;
        for (int i = 0; i < leftList.size(); i++) {
            boolean flag = true;
            String str = (String) leftList.get(i);
            for (j = 0; j < rightList.size(); j++) {
                String str2 = (String) rightList.get(j);
                if (str.equals(str2)) {
                    flag = false;
                }
            }
            if (flag == true) {
                arrayListthree.add(str);
            }
        }
        return arrayListthree;
    }
    public static void main(String args[]) {
        DealString.getCharArray("0000111", 4);
    }
}


IT家园
IT家园

网友最新评论 (0)