截取字符串str中指定字符

/**
 * 截取字符串str中指定字符 strStart、strEnd之間的字符串
 */
public static String subString(String str, String strStart, String strEnd) {
    /* 找出指定的2個字符在 該字符串裏面的 位置 */
    int strStartIndex = str.indexOf(strStart);
    int strEndIndex = str.indexOf(strEnd);
    /* index 爲負數 即表示該字符串中 沒有該字符 */
    if (strStartIndex < 0) {
        return "字符串 :---->" + str + "<---- 中不存在 " + strStart + ", 無法截取目標字符串";
    }
    if (strEndIndex < 0) {
        return "字符串 :---->" + str + "<---- 中不存在 " + strEnd + ", 無法截取目標字符串";
    }
    /* 開始截取 */
    String result = str.substring(strStartIndex, strEndIndex).substring(strStart.length());
    return result;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章