String 常用的字符串共通处理归纳一

可做成CommonFunctions的一些常用的字符串共通处理归纳一:字符串处理相关

 

/**
 * nullToBlank() : 参数为null的场合,转化成空
 * 
 * @param pValue : 字符串型参数
 * @return : null -> ""
 */
public static String nullToBlank(String pValue) {
    if(pValue == null || pValue.toLowerCase().equals("null")) {
        return "";
    } else {
        return pValue;
    }
}

 

/**
 * nullToZero() : 参数为null的场合,转化成0
 * 
 * @param pValue : 参数
 * @return : null -> 0
 */
public static String nullToZero(String pValue) {
    if (pValue == null || pValue.toLowerCase().equals("null") || pValue.equals("")) {
        return "0";
    } else {
        return pValue;
    }
}

 

/**
 * splitToArray() : 将字符串按目标字符切分成数组后返回
 *
 * @param pTargetStr : 字符串
 * @param pSplitFlg : 切分字符
 * @return : 数组
 */
public static String[] splitToArray(String pTargetStr, String pSplitFlg) {
    int index = -1;
    ArrayList<String> strArray = new ArrayList<String>();

    while ((index = pTargetStr.indexOf(pSplitFlg)) != -1) {
        strArray.add(pTargetStr.substring(0, index));
        pTargetStr = pTargetStr.substring(index + pSplitFlg.length());
    }

    strArray.add(pTargetStr);
    String[] retStr = (String[]) strArray.toArray(new String[strArray.size()]);

    return retStr;
}

 

/**
 * splitToVector() : 将字符串按目标字符切分成数组后返回
 *
 * @param pTargetStr : 字符串
 * @param pSplitFlg : 切分字符
 * @return : 数组
 */
public static Vector<String> splitToVector(String pTargetStr, String pSplitFlg) {

    Vector<String> retVal = null;
    String[] arrayText = splitToArray(pTargetStr, pSplitFlg);
    if (arrayText.length > 0) {
        retVal = new Vector<String>(arrayText.length);

        for (int i = 0; i < arrayText.length; i++) {
            retVal.addElement(arrayText[i]);
        }
    }
    return retVal;
}

 

/**
 * joinToString() : 将目标数组按指定字符切分,合成字符串后返回
 *
 * @param pVecOriginal : 数组
 * @param pSplitFlg : 切分字符
 * @return : 字符串
 */
public static String joinToString(Vector<String> pVecOriginal, String pSplitFlg) {
    String retVal = "";
    for (int i = 0; i < pVecOriginal.size(); i++) {
        retVal = retVal + pSplitFlg + pVecOriginal.elementAt(i).toString();
    }

    if (!(retVal.equals(""))) {
        retVal = retVal.substring(1);
    }

    return nullToBlank(retVal);
}

 

/**
 * paddingZeroL() : 在目标字符串的前面补齐0后返回
 *
 * @param pOriginalData : 字符串
 * @param paddingLength : 补齐后字符串长度
 * @return : 补齐后字符串
 */
public static String paddingZeroL(String pOriginalData, long pAllLength) {

    String retVal = pOriginalData;
    byte[] byteOfString = retVal.getBytes();
    long cnt = pAllLength - byteOfString.length;

    for (int i = 0; i < cnt; i++) {
        retVal = "0" + retVal;
    }

    return nullToBlank(retVal);
}

 

/**
 * paddingZeroR() : 在目标字符串的后面补齐0后返回
 *
 * @param pOriginalData : 字符串
 * @param paddingLength : 补齐后字符串长度
 * @return : 补齐后字符串
 */
public static String paddingZeroR(String pOriginalData, long pAllLength) {
    String retVal = pOriginalData;
    byte[] byteOfString = retVal.getBytes();
    long cnt = pAllLength - byteOfString.length;

    for (int i = 0; i < cnt; i++) {
        retVal = retVal + "0";
    }

    return nullToBlank(retVal);
}
 
public static final String PRE = "PRE";
public static final String END = "END";

/**
 * addStrPreOrEnd() : 在目标字符串的前面/后面补齐后返回
 *
 * @param pTargetStr : 目标字符串
 * @param pAddFlg : 前面/后面
 * @param pLength : 需要补齐的位数
 * @param pAddStr : 需要补齐的字符串
 * @return : 补齐后字符串
 */
public static String addStrPreOrEnd(String pTargetStr, String pAddFlg, int pLength, String pAddStr) {
    String strAppend = "";
    for (int i = 0; i < pLength; i++) {
        strAppend += pAddStr;
    }

    if (pAddFlg.equals(PRE)) {
        return strAppend + pTargetStr;

    } else if (pAddFlg.equals(END)) {
        return pTargetStr + strAppend;

    } else {
        return pTargetStr;
    }
}
 
/**
 * cutHeadderZero() : 将目标字符串的前面的0去掉后返回
 *
 * @param pTargetStr : 目标字符串
 * @return : 去0后的字符串
 */
public static String cutHeadderZero(String pTargetStr) {
    String retVal = pTargetStr;
    String temp = pTargetStr;

    while (retVal.length() > 0) {
        if (retVal.substring(0, 1).equals("0")) {
            retVal = retVal.substring(1);
        }

        if (retVal.equals(temp)) {
            break;
        }

        temp = retVal;
    }

    return retVal;
}
 
/**
 * getByteLengthByEncoding() : 按字符编码来计算字符串长度
 *
 * @param pTargetStr : 字符串
 * @param pEncoding : 编码(MS932/...)
 * @return : 字符串长度
 */
public static int getByteLengthByEncoding(String pTargetStr, String pEncoding) {
    try {
        if (pTargetStr == null) {
            return 0;
        }
        byte[] b = pTargetStr.getBytes(pEncoding);
        return b.length;

    } catch (java.io.UnsupportedEncodingException e) {
        return 0;
    }
}
 
/**
 * getByteLengthByCode() : 按Unicode/Shift_JIS来计算字符串长度
 *
 * @param pTargetStr : 字符串
 * @return : 字符串长度
 */
public static int getByteLengthByCode(String pTargetStr) {
    int lengthCnt = 0;

    for (int i = 0; i < pTargetStr.length(); i++) {
        int c = pTargetStr.charAt(i);
        // Shift_JIS: 0x0 ~ 0x80, 0xa0  , 0xa1   ~ 0xdf  , 0xfd   ~ 0xff
        // Unicode  : 0x0 ~ 0x80, 0xf8f0, 0xff61 ~ 0xff9f, 0xf8f1 ~ 0xf8f3
        if ( (c >= 0x0 && c < 0x81) || (c == 0xf8f0) || (c >= 0xff61 && c < 0xffa0) || (c >= 0xf8f1 && c < 0xf8f4)) {
            lengthCnt += 1;
        } else {
            lengthCnt += 2;
        }
    }

    return lengthCnt;
}

 

/**
 * getKeepLimitStr() : 当字符串超出指定长度时,以省略号形式显示
 *
 * @param pTargetStr : 字符串
 * @param pLength : 指定长度
 * @return : 字符串
 */
public static String getKeepLimitStr(String pTargetStr, int pLength) {

    int orgLength = getByteLengthByEncoding(pTargetStr, "MS932");

    if (orgLength > pLength) {
        pLength -= 2;
        int index = pLength / 2;
        String retVal = pTargetStr.substring(0, index);

        while (getByteLengthByEncoding(retVal, "MS932") < pLength) {
            retVal = retVal + pTargetStr.substring(index, index + 1);
            index++;
        }

        if (getByteLengthByEncoding(retVal, "MS932") > pLength) {
            retVal = retVal.substring(0, retVal.length() - 1);
        }

        return retVal + "…";
    }

    return pTargetStr;
}

 

 

你可能感兴趣的:(字符串处理)