首页 > 开发 > .Net > 正文

转贴自JoyASP:.NET框架(二)

2020-02-03 15:53:57
字体:
来源:转载
供稿:网友
一、处理字符串
.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)


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表