JSTL學習小結——對fn:substring()方法的理解

在JSTL標準標籤庫中,函數標籤庫fn:substring()方法比較特殊,語法爲:

【語法1】${fn:substring(<string>,<beginIndex>,<endIndex>)}

【功能從字符串string中,截取從起始索引值開始(包含),到終止索引值結束(不包含)的子字符串。

  該標籤類似Excel中的MID函數,其語法結構爲:

【語法2】MID(text, start_num, num_chars)

其中start_num相當於這裏的beginIndex,只是起始編號爲1;num_chars代表子文本的長度。

  對於fn:substring()方法,網上許多資料以字符串“This is first String.”爲例,演示了${fn:substring("This is first String.", 5, 15)}的運行結果,但並未提示“子字符串是不包含終止索引值對應的字符元素”這一要點,在此特別強調一下。個人認爲,該標籤語法結構若改爲如下形式更便於理解:

【語法3】${fn:substring(<string>,<beginIndex>,<beginIndex>+<substringLength>)}

這樣,參數3就變成了子字符串首字符索引值beginIndex與子串總長substringLength的和,這樣對於${fn:substring("This is first String.", 5, 15)}就可以理解爲:從原字符串索引值爲5的字符開始,截取10個字符(=15-5)所形成的子字符串。

  此外通過代碼還發現了該方法一些有趣的結論,先上代碼:

<body>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
	<h1>JSTL函數標準標籤庫之fn:substring()方法示例</h1>
	<h3>語法:<%="${fn:substring(<string>, <beginIndex>, <endIndex>)}" %></h3>
	<hr>
	<c:set var="strDemo" value="This is first String."/>
	<h3>原字符串爲:<c:out value="${strDemo}"/></h3>
	<h3><%="${fn:substring(strDemo, 5, 0)}" %> = 
		"<c:out value="${fn:substring(strDemo, 5, 0)}" />"</h3>
	<h3><%="${fn:substring(strDemo, 5, -0.4)}" %> = 
		"<c:out value="${fn:substring(strDemo, 5, -0.4)}" />"</h3>
	<h3><%="${fn:substring(strDemo, 5, -0.5)}" %> = 
		"<c:out value="${fn:substring(strDemo, 5, -0.5)}" />"</h3>
	<h3><%="${fn:substring(strDemo, 5, -0.6)}" %> = 
		"<c:out value="${fn:substring(strDemo, 5, -0.6)}" />"</h3>
	<h3><%="${fn:substring(strDemo, 5, -1.0)}" %> = 
		"<c:out value="${fn:substring(strDemo, 5, -1.0)}" />"</h3>
	<h3><%="${fn:substring(strDemo, 5, -1.4)}" %> = 
		"<c:out value="${fn:substring(strDemo, 5, -1.4)}" />"</h3>
	<h3><%="${fn:substring(strDemo, 5, -1.5)}" %> = 
		"<c:out value="${fn:substring(strDemo, 5, -1.5)}" />"</h3>
	<h3><%="${fn:substring(strDemo, 5, -1.6)}" %> = 
		"<c:out value="${fn:substring(strDemo, 5, -1.6)}" />"</h3>
	<h3><%="${fn:substring(strDemo, 5, 15.0)}" %> = 
		"<c:out value="${fn:substring(strDemo, 5, 15.0)}" />"</h3>
	<h3><%="${fn:substring(strDemo, 5, 15.4)}" %> = 
		"<c:out value="${fn:substring(strDemo, 5, 15.4)}" />"</h3>
	<h3><%="${fn:substring(strDemo, 5, 15.5)}" %> = 
		"<c:out value="${fn:substring(strDemo, 5, 15.5)}" />"</h3>
	<h3><%="${fn:substring(strDemo, 5, 15.6)}" %> = 
		"<c:out value="${fn:substring(strDemo, 5, 15.6)}" />"</h3>
</body>

運行結果:


測試結論如下:

1、fn:substring方法是按首尾索引值截取子字符串,且包含子串的首字符索引值,不含尾字符索引值;

2、第三參數可以是0、小數、負數。
 2.1)endIndex = 0,得到空字符串"";
 2.2)endIndex爲( 0, +∞ )內的小數,截尾取整後得到對應的結果,即${fn:substring(string,5,15.5)}與${fn:substring(string,5,15)}是等效的;
 2.3)endIndex∈( -1, 0 ),效果同endIndex=0,得到空字符串"";
 2.4)endIndex∈( -∞, -1 ],將得到beginIndex及其後所有內容,相當於endIndex=(string.length+1);

3、進一步測試可以發現,第二參數beginIndex也可以是負數、小數,但爲負數時與爲0時等效,小數也是結尾取整得到對應結果;

4、原字符串的index值是從0開始的。


參考資料:

http://www.runoob.com/jsp/jstl-function-substring.html

http://www.tutorialspoint.com/jsp/jstl_function_substring.htm


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章