java如何截取2个字符之间的字符串呢?
									
下文笔者讲述提取字符串中特定位置间的字符的方法分享,如下所示
				 
				
实现思路:
    获取字符指定的索引位置
	然后使用substring方法截取字符串
例:
/**
 * 截取字符串str中指定字符 strStart、strEnd之间的字符串
 * 
 * @param string
 * @param str1
 * @param str2
 * @return
 */
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;
}
//例:超简介的字符串截取方式
-------截取指定2个字符之间的字符串
public String splitData(String str, String strStart, String strEnd) {
        String tempStr;
        tempStr = str.substring(str.indexOf(strStart) + 1, str.lastIndexOf(strEnd));
        return tempStr;
    }
									
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

			
               
               
               
               
          
