轉貼自JoyASP:.NET框架(二)
2024-07-10 12:59:56
供稿:網友
 
一、處理字符串 
.net 框架類(或system類)提供了大量可在構造.net應用時使用的核心功能,這些功能適用于任何語言環境。本文的第一部分介紹了程序集、名稱空間等基本概念以及system.math和system.random類。這是本文的第二部分,接著討論其他幾個很有用的類:system.string,system.array,system.datetime。 
值得指出的是,正如本文前面所提到的,當我們用vb.net作為編程語言時,我們常常面臨這樣一種選擇:是使用vb.net語言內建的功能,還是使用等價的system類功能。在處理數組、日期/時間、字符串數據時,我們就面臨這種選擇。如果你曾經是一個vb 6.0程序員,你的第一個選擇可能會是經過檢驗的、確實有效的老方法。但是,如果有可能,你最好還是改掉老習慣、采用新的.net system類。為什么呢?因為采用system類能夠使你的代碼更容易移植到其他.net語言以及未來的vb.net版本。 
system.string類提供了豐富的字符串處理能力。使用system.string類,我們可以:確定字符串的長度,查找子串,改變字符串的大小寫,比較兩個字符串,分割字符串,等等。 
確定字符串長度使用的是length屬性。例如,在下面的代碼中,intlength的值將是4: 
dim strcolor as string = "blue"
dim intlength as integer
intlength = strcolor.length
我們用indexof方法從字符串找出第一個匹配的子串。如果能夠找到子串,indexof方法返回子串的開始位置(第一個字符的開始位置為0);如果不能找到,則indexof返回-1。indexof的查找是大小寫敏感的。indexof是一個被重載(overload)的方法,它允許傳入的參數包括:char類型的字符,string類型的字符串,char類型的字符數組。下面這個indexof.aspx頁面示范了三種不同參數類型indexof方法的運用: 
<%@ page language="vb" explicit="true"%>
<head>
<title>system.string實例</title>
<script language="vb" runat="server">
sub page_load(src as object, e as eventargs)
    dim chrg as char = "g"
    dim strword as string = "for"
    dim chrvowels as char() = {"a","e","i","o","u"}
    dim strphrase as string = _
     "one small step for man, one giant leap for mankind."
    dim i as integer
    
    lbloutput.text &= "<br />strphrase = " & strphrase
    lbloutput.text &= "<br />position of chrg = " _
     & strphrase.indexof(chrg)
    lbloutput.text &= "<br />position of strword = " _
     & strphrase.indexof(strword)
    lbloutput.text &= "<br />position of chrvowels = " _
     & strphrase.indexof(chrvowels)
end sub 
</script>
</head>
<body>
<asp:label id="lbloutput" runat="server" />
</body>
</html>
這個頁面的運行結果如下: 
indexof允許指定兩個用來限制搜索的可選參數,它們分別代表搜索字符串的起始和結束位置。例如,下面的代碼對chrvowels的搜索限制在第10到20個字符之間: 
strphrase.indexof(chrvowels, 10, 20)
lastindexof方法類似于indexof方法,但它搜索的是子串的最后一次出現。例如,如果你修改indexof.aspx頁面,用lastindexof方法來取代indexof方法,則strword的位置將是39而不是15。 
使用system.string的toupper和tolower方法可以把字符串分別改成全部大寫或者全部小寫。例如: 
strupper = "this is a mixed case sentence".toupper()
strlower =  "this is a mixed case sentence".tolower()
從這個例子可以看出,system.string的屬性和方法既可以在字符串變量中應用,也可以直接在字符串文本中應用。 
你可以用compare方法比較兩個字符串是否相同。如果兩個字符串相同,compare方法返回0;如果第一個字符串小于第二個字符串,compare返回一個負數;如果第一個字符串大于第二個字符串,compare方法返回一個正數。compare是一個靜態方法(參見本文前面關于靜態方法和實例方法的說明)。默認情況下,compare對字符串的比較是大小寫敏感的,且不考慮地區關系。例如,下面對str1和str2的比較將返回-1,它表示str1小于str2: 
dim str1 as string = "abcd.com"
dim str2 as string = "abcd.com"
answer = string.compare(str1, str2)
我們可以向compare傳入第三個可選的參數。如果第三個參數指定為true,則字符串比較操作忽略大小寫,比如下面的代碼中answer的值將是0,即兩個字符串相等。 
answer = string.compare(str1, str2, true)
正如indexof方法,compare也是一個被重載的方法。我們可以向compare方法傳入第四個參數要求進行地區相關的比較;或者,我們也可以指定字符的起始和結束位置使得比較只對字符串的一部分進行。請參見.net framework sdk文檔了解詳細信息。split方法把字符串分割成一個由子串構成的數組。使用split方法時,我們必須指定用來分割字符串的、char類型的分割字符。下面的split.aspx頁面示范了split方法的應用: 
<%@ page language="vb" explicit="true"%>
<head>
<title>split實例</title>
<script language="vb" runat="server">
sub page_load(src as object, e as eventargs)
    dim strasp as string = _
         "asp.net is the next generation of active server pages."
    dim strwords() as string
    dim i as integer
    strwords = strasp.split(" ")
    for i = strwords.getlowerbound(0) to strwords.getupperbound(0)
        lbloutput.text &= i & ": " & strwords(i) & "<br />"
    next
end sub 
</script>
</head>
<body>
<asp:label id="lbloutput" runat="server" />
</body>
</html>
split.aspx的輸出結果如下: 
前面我們討論了string類部分屬性和方法的應用。string還包括許多其他成員,比如:從數組構造出字符串,把字符串中的一個字符替換成其他字符,刪除字符串前面或者后面的空白字符,等等。 
二、操作數組 
我們可以通過system.array類用各種方法處理數組。與前面的幾個類一樣,system.string類的許多功能重復了vb語言所具有的功能。但array類也增加了一些傳統vb語言不具備的功能,比如搜索和排序數組。 
array類的getlowerbound和getupperbound方法用于確定數組指定維的下界和上界。下面這個語句來自split.aspx(參見前面的例子),它通過getlowerbound和getupperbound方法確定strwords數組的邊界: 
for i = strwords.getlowerbound(0) to strwords.getupperbound(0)
system.array的sort靜態方法能夠對一維數組的內容排序。sort方法對數組的排序是大小寫敏感的,而且它不能對一維以上的數組排序。調用sort方法的語法下: 
array.sort(array_name)
對于一維數組,我們還可以用reverse方法顛倒數組元素的次序。reverse方法的語法類似于sort方法: 
array.reverse(array_name)
下面的代碼(來自arraysort.aspx示例頁面)示范了sort和reverse方法的應用: 
dim strterms() as string = {"jscript", "vb", "asp", "asp.net", ".net"}
dim i as integer
lbloutput.text &= "original array<br />"
for i = strterms.getlowerbound(0) to strterms.getupperbound(0)
    lbloutput.text &= i & ": " & strterms(i) & "<br />"
next
array.sort(strterms)
lbloutput.text &= "<br />after sorting<br />"
for i = strterms.getlowerbound(0) to strterms.getupperbound(0)
    lbloutput.text &= i & ": " & strterms(i) & "<br />"
next
array.reverse(strterms)
lbloutput.text &= "<br />after reversing<br />"
for i = strterms.getlowerbound(0) to strterms.getupperbound(0)
    lbloutput.text &= i & ": " & strterms(i) & "<br />"
next
arraysort.aspx頁面的輸出結果如下: 
system.array方法支持用indexof和lastindexof方法對一維數組進行搜索,這兩個方法與system.string類的同名方法類似。用indexof和lastindexof方法搜索數組的語法如下: 
answer = array.indexof(array_name, search_string)
answer = array.lastindexof(array_name, search_string)
這兩個方法分別返回搜索字符串第一次和最后一次匹配的位置;如果不能找到,則返回值是-1。這種搜索是大小寫敏感的。例如,在下面的代碼中,answer將是2,它表示字符串“asp”是strterms數組的第三個元素。 
dim strterms() as string = {"jscript", "vb", "asp", "asp.net", ".net"}
answer = array.indexof(strterms, "asp")
三、處理日期/時間數據 
system.datetime類提供了許多處理datetime值的方法。要創建一個datetime值,我們只需聲明一個datetime類型的變量,并通過“#”分隔符賦予它一個datetime常量,如下所示: 
dim seattlequake as datetime = #02/28/01 10:54#
system.datetime類一個很大的優點是:我們能夠通過它的屬性非常方便地分析日期/時間值。這些datetime類屬性的含義非常明顯,它們是:year,month,day,dayofweek,dayofyear,hour,minute,second,millisecond,ticks,等。每個ticks等于100個納秒(毫微秒)。例如,在下面的代碼中,answer的值將等于10: 
answer = seattlequake.hour
我們還可以用date和timeofday屬性獲得datetime數據的日期或者時間部分。timeofday屬性返回的是一個timespan值,它表示已流逝的按ticks計的時間。可以想象,利用timespan值的屬性我們可以分析出timespan時間的各個部分。請參見.net framework sdk文檔了解詳細信息。 
system.datetime類還提供了幾個增加(或者減少)datetime值某一部分的方法,它們是:addyears,addmonths,adddays,addhours,addminutes,addseconds,addmilliseconds,addticks。 
例如,下面的代碼對指定的日期(bday)進行加1年、減1年操作: 
dim bday as datetime = #6/25/2001 12:00#
dim nextbday as datetime
dim lastbday as datetime
nextbday = thedate.addyears(1)
lastbday = thedate.addyears(-1)