点此进入千百惠旗舰店点此进入天猫商城2010-2011部分工程样板展示及新款式推广. 使用数学函数 system.math 类中定义了用于数学计算的函数.math 类包括三角函数,对数函数和其他常用数学函数.下列函数是在 system 名称空间的 math 类中定义的函数. 注意:要使用这些函数,请通过在源代码顶部添加以下代码将 system.math 名称空间导入到项目中: imports system.math abs abs 函数返回指定数值的绝对值. abs 示例: 本示例使用 math 类的 abs 方法来计算一个数值的绝对值. code will not compile nless it is p t in a s b or in a f nction. dim myn mber as do ble myn mber = abs(50.3) ret rns 50.3. myn mber = abs(-50.3) ret rns 50.3.atan atan 函数返回包含角度的 do ble 值.该角度的正切值是指定的数值.返回值为正表示角度在 x 轴的逆时针方向上.返回值为负表示顺时针角度.将该返回值乘以 180 再除以 pi (p) 就可以从弧度转换为度. atan 示例: 本示例使用 math 类的 atan 方法来计算 pi 值. code is not compiled nless it is p t in a s b or in a f nction. dim pi as do ble pi = 4 * atan(1) calc late the val e of pi.cos cos 函数以弧度表示的角度作为参数,然后返回一个 do ble 值,表示指定角度的余弦值. cos 示例: 本示例使用 math 类的 cos 方法返回角度的余弦值. code is not compiled nless it is p t in a s b or in a f nction. dim myangle, mysecant as do ble myangle = 1.3 define angle in radians. mysecant = 1  cos(myangle) calc late secant.exp exp 函数返回一个 do ble 值,包含 e(自然对数的底)的指定乘幂.使用 pow 方法可计算其他底的乘幂.exp 是 log 的逆运算. exp 示例: 本示例使用 math 类的 exp 方法返回 e 的乘幂. code is not compiled nless it is p t in a s b or in a f nction. dim myangle, myhsin as do ble define angle in radians. myangle = 1.3 calc late hyperbolic sine. myhsin = (exp(myangle) - exp(-1 * myangle))  2log log 函数返回一个 do ble 值,包含一个指定数值的对数.该方法已重载,可以返回指定数值的自然(底数 e)对数或指定数值的指定底对数. log 示例: 本示例使用 math 类的 log 方法返回一个数值的自然对数. code is not compiled nless it is p t in a s b or in a f nction. dim myangle, mylog as do ble define angle in radians. myangle = 1.3 calc late inverse hyperbolic sine. mylog = log(myangle + sqrt(myangle * myangle + 1))ro nd ro nd 函数返回一个 do ble 值,包含与指定值最接近的数值.其他 ro nd 函数可用作内部类型的方法,如  nd 方法. ro nd 示例: 本示例使用 math 类的 ro nd 方法将一个数值四舍五入为最接近的整数. code is not compiled nless it is p t in a s b or in a f nction. dim myvar1 as do ble = 2.8 dim myvar2 as do ble myvar2 =ro nd(myvar1) ret rns 3.sign sign 函数返回一个指示某个数值的正负号的整数值.下面的表格显示了函数及其返回值的输入参数: 指定数 返回值 正数 1 负数 -1 零 0 sign 示例: 本示例使用 math 类的 sign 方法确定一个数值的正负号. code is not compiled nless it is p t in a s b or in a f nction. dim myvar1, myvar2, myvar3 as do ble dim mysign as integer myvar1 = 12 myvar2 = -2.4 myvar3 = 0 mysign = sign(myvar1) ret rns 1. mysign = sign(myvar2) ret rns -1. mysign = sign(myvar3) ret rns 0.sin sin 函数以弧度表示的角度作为参数,然后返回一个 do ble 值,指定该角度的正弦值. sin 示例: 本示例使用 math 类的 sin 方法返回一个角度的正弦值. code is not compiled nless it is p t in a s b or in a f nction. dim myangle, mycosecant as do ble myangle = 1.3 define angle in radians. mycosecant = 1  sin(myangle) calc late cosecant.sqrt sqrt 函数返回一个 do ble 值,指定一个指定数值的平方根. sqrt 示例: 本示例使用 math 类的 sqrt 方法计算一个数值的平方根. code is not compiled nless it is p t in a s b or in a f nction. dim mysqr as do ble mysqr = sqrt(4) ret rns 2. mysqr = sqrt(23) ret rns 4.. mysqr = sqrt(0) ret rns 0. mysqr = sqrt(-4) ret rns nan (not a n mber).tan tan 函数返回一个 do ble 值,包含指定角度的正切值.tan 函数以弧度表示的角度作为参数.当指定的角度是 nan,negativeinfinity 或 positiveinfinity 时,该方法将返回 nan. 注意:乘以 p180 可将度数转换为弧度数. tan 示例: 本示例使用 math 类的 tan 方法返回一个角度的正切值. code is not compiled nless it is p t in a s b or in a f nction. dim myangle, mycotangent as do ble myangle = 1.3 define angle in radians. mycotangent = 1  tan(myangle) calc late cotangent.使用类型转换函数 将一个值从一种数据类型更改为另一种数据类型的过程称为转换.转换或者是扩大或者是收缩,这取决于所涉及的类型的数据容量.公共语言运行库支持扩大转换和收缩转换两种.例如,以 32 位有符号整数形式表示的值可以转换为 64 位有符号整数.这是一个扩大转换的示例.相反的转换(从 64 位到 32 位)是一个收缩转换的示例.使用扩大转换信息永远不会丢失.但是,使用收缩转换信息可能会丢失. 以下是 vis al basic .net 中定义的类型转换函数: cbool cbool 函数用于将字符串表达式或数值表达式转换为 boolean 值.当表达式计算为一个非零值时,cbool 函数将返回 tr e.否则,该函数返回 false. cbool 示例: dim a, b, c as integer dim check as boolean a = 5 b = 5 check = cbool(a = b) check is set to tr e. c = 0 check = cbool(c) check is set to false. cbyte cbyte 函数将指定数值转换为字节.输入参数必须是 0 到 255 之间的一个数值.否则,将出现 system.overflowexception. cbyte 示例: dim mydo ble as do ble dim mybyte as byte mydo ble = 125.5678 mybyte = cbyte(mydo ble) mybyte is set to 126. cchar cchar 函数仅转换指定字符串的第一个字符.cchar 的输入参数必须是数据类型的字符串.无法使用 cchar 将数值转换为字符,因为 cchar 无法接受 n meric 数据类型. cchar 示例: 本示例使用 cchar 函数将字符串表达式的第一个字符转换为 char 类型. dim mystring as string dim mychar as char mystring = "bcd" cchar converts only the first character of the string. mychar = cchar(mystring) mychar is set to "b". cdate cdate 接受日期和时间的任何有效表示方式,然后将其转换为 date 值. cdate 示例: 本示例使用 cdate 函数将字符串转换为 date 值. dim mydatestring, mytimestring as string dim mydate, mytime as date mydatestring = "febr ary 12, 1969" mytimestring = "4:35:47 pm" ... mydate = cdate(mydatestring) convert to date data type. mytime = cdate(mytimestring) convert to date data type. cdbl cdbl 函数用于将数值表达式转换为 do ble 值.对于负值,该函数的输入参数必须在 -4.e-324 到 -1.e+308 之间.对于正值,该函数的输入参数必须在 1.e+308 到 4.e-324 之间. cdbl 示例: dim mydec as decimal dim mydo ble as do ble mydec = 234.d literal type character d makes mydec a decimal. mydo ble = cdbl(mydec * 8.2d * 0.01d) convert res lt to a do ble. cdec cdec 函数将数值转换为小数. cdec 示例: dim mydo ble as do ble dim mydecimal as decimal mydo ble = .0587 mydecimal = cdec(mydo ble) convert to decimal. cint cint 函数将数值转换为整数. cint 示例: dim mydo ble as do ble dim myint as integer mydo ble = 2345.5678 myint = cint(mydo ble) myint is set to 2346. clng clng 函数以一个数值作为参数,然后返回一个 long 值. clng 示例: dim mydbl1, mydbl2 as do ble dim mylong1, mylong2 as long mydbl1 = .45 mydbl2 = .55 mylong1 = clng(mydbl1) mylong1 contains . mylong2 = clng(mydbl2) mylong2 contains . cobj cobj 函数将数值转换为对象. cobj 示例: dim mydo ble as do ble dim myobject as object mydo ble = 2. myobject = cobj(mydo ble) do ble val e is pointed to by myobject. cshort cshort 函数将数值转换为 short 值. cshort 示例: dim mybyte as byte dim myshort as short mybyte = 100 myshort = cshort(mybyte) convert to short. csng csng 函数将数值转换为 single 值. csng 示例: dim mydo ble1, mydo ble2 as do ble dim mysingle1, mysingle2 as single mydo ble1 = 75. mydo ble2 = 75. mysingle1 = csng(mydo ble1) mysingle1 is set to 75.. mysingle2 = csng(mydo ble2) mysingle2 is set to 75.. cstr 下面的表格显示了 cstr 函数的输入参数和返回值: 输入参数数据类型 返回值 boolean 包含 tr e 或 false 的字符串 date 包含以系统的短日期格式表示的 date 值(日期和时间)的字符串 数值 表示数字的字符串 cstr 示例: 本示例使用 cstr 函数将数值转换为字符串. dim mydo ble as do ble dim mystring as string mydo ble = 437.324 mystring = cstr(mydo ble) mystring is set to "437.324". 使用字符串函数 不同的类中都定义有字符串函数.这些类包括 microsoft.vis albasic.strings 类和 system.string 类. 使用 microsoft.vis albasic.strings 类中的字符串函数 下列函数是 microsoft.vis albasic.strings 类中定义的字符串函数. 注意:要使用字符串函数,请通过在源代码开始处添加以下代码将名称空间 microsoft.vis albasic.strings 导入到项目中: imports microsoft.vis albasic.strings asc 和 ascw asc 函数和 ascw 函数返回一个整数值,表示与指定的字符相对应的字符代码.这两个函数接受任何有效的字符表达式或字符串表达式作为参数.当字符串是输入参数时,则仅输入字符串的第一个字符.当字符串不包含任何字符时,将出现 arg mentexception 错误.asc 返回输入字符的代码数据点或字符代码.对于单字节字符集 (sbcs) 值,返回值可以是 0 到 255 之间的数字.对于双字节字符集 (dbcs) 值,返回值可以是 - 到  之间的数字.ascw 为输入字符返回 0 到  之间的 unicode 代码数据点. 例如: dim myint as integer myint = asc("a") myint is set to 65. myint = asc("a") myint is set to 97. myint = asc("apple") myint is set to 65. chr 和 chrw chr 函数和 chrw 函数返回与指定的字符代码相关联的字符.当 charcode 超出 - 到  的范围时,将出现 arg mentexception 错误. 例如: 本示例使用 chr 函数返回与指定的字符代码相关联的字符. dim mychar as char mychar = chr(65) ret rns "a". mychar = chr(97) ret rns "a". mychar = chr(62) ret rns "". mychar = chr(37) ret rns "%". getchar getchar 函数返回一个 char 值,表示指定字符串的指定索引中的字符.当索引小于 1 或大于指定输入参数中最后一个字符的索引时,将出现 arg mentexception 错误. 例如: 本示例显示了如何使用 getchar 函数从字符串的指定索引中返回字符. dim mystring as string = "abcde" dim mychar as char mychar = getchar(mystring, 4) mychar = "d" instr instr 函数返回一个整数,指定一个字符串在另一个字符串中首次出现的起始位置. 例如: 以下示例使用 instr 函数返回一个字符串在另一个字符串中首次出现的位置: dim searchstring, searchchar as string dim mypos as integer searchstring ="xxpxxpxxpxxp" string to search in. searchchar = "p" search for "p". a text al comparison starting at position 4. ret rns 6. mypos = instr(4, searchstring, searchchar, comparemethod.text) join join 函数返回一个字符串,该字符串是通过连接数组中包含的子字符串创建的.包含必须连接的子字符串的一维数组将作为参数传递给 join 函数.该函数使用 delimiter,string 作为可选参数来分隔返回的字符串中的子字符串.当省略 delimiter 时,将使用空格(" ")作为子字符串之间的分隔符.当 delimiter 是零长度字符串 ("") 时,数组中的子字符串将不使用分隔符,而是直接相连. 例如: 以下示例显示了如何使用 join 函数: dim myitem(2) as string dim myshoppinglist as string myitem(0) = "pickle" myitem(1) = "pineapple" myitem(2) = "papaya" ret rns "pickle, pineapple, papaya" myshoppinglist = join(myitem, ", ") lcase lcase 函数返回已经转换为小写的字符串或字符.只有大写字母被转换为小写.所有小写字母和非字母字符均保持不变. 例如: 以下示例使用 lcase 函数返回字符串的小写形式: dim uppercase, lowercase as string uppercase = "hello world 1234" string to convert. lowercase = lcase(uppercase) ret rns "hello world 1234". ltrim,rtrim 和 trim 这些函数会返回一个包含指定字符串的副本的字符串.在使用 ltrim 时,没有起始空格.在使用 rtrim 时,没有尾随空格.在使用 trim 时,既没有起始空格也没有尾随空格. 例如: 以下示例使用 ltrim 函数删除字符串变量中的起始空格,使用 rtrim 函数删除字符串变量中的尾随空格,以及使用 trim 函数删除字符串变量中的起始空格和尾随空格: dim mystring, trimstring as string mystring = " -trim- " initializes string. trimstring = ltrim(mystring) trimstring = "-trim- ". trimstring = rtrim(mystring) trimstring = " -trim-". trimstring = ltrim(rtrim(mystring)) trimstring = "-trim-". using the trim f nction alone achieves the same res lt. trimstring = trim(mystring) trimstring = "-trim-". replace replace 函数返回一个字符串,其中指定的子字符串按指定的次数替换为另一个子字符串.replace 函数的返回值是一个字符串,该字符串在 start 参数指定的位置开始,然后在指定字符串的末尾以 find 参数和 replace 参数中的值所指定的替换内容结束. 例如: 本示例演示了 replace 函数: dim mystring as string = "shopping list" dim astring as string ret rns "shipping list". astring = replace(mystring, "o", "i") strcomp strcomp 函数返回 1,0 或 1.这将基于字符串比较的结果.字符串将从第一个字符开始按字母数字顺序排列的值进行比较. 例如: 以下示例使用 strcomp 函数返回字符串比较的结果.如果省略第三个参数,则使用选项比较语句或项目默认设置中定义的比较类型. dim mystr1, mystr2 as string dim mycomp as integer mystr1 = "abcd" mystr2 = "abcd" defines variables. the two strings sort eq ally. ret rns 0. mycomp = strcomp(mystr1, mystr2, comparemethod.text) mystr1 sorts after mystr2. ret rns -1. mycomp = strcomp(mystr1, mystr2, comparemethod.binary) mystr2 sorts before mystr1. ret rns 1. mycomp = strcomp(mystr2, mystr1) strconv strconv 函数返回一个字符串,该字符串转换为输入参数中指定的值.strconv 函数将转换字符串.这种转换基于 conversion 参数中的值.conversion 参数中的值是 vbstrconv 枚举的成员. conversion 参数的设置为: 枚举成员 说明 vbstrconv.none 不执行转换 vbstrconv.ling isticcasing - 使用语言规则而不是文件系统(默认值)来区分大小写 - 仅对大写和小写字母有效 vbstrconv.uppercase 将字符串转换为大写字符 vbstrconv.lowercase 将字符串转换为小写字符 vbstrconv.propercase 将字符串中每个单词的第一个字母转换为大写 例如: 以下示例将文本转换为小写字母: dim stext, snewtext as string stext = "hello world" snewtext = strconv(stext, vbstrconv.lowercase) deb g.writeline (snewtext) o tp ts "hello world". strd p strd p 函数返回一个由指定的字符重复指定的次数而形成的字符串或对象.strd p 函数具有两个参数:n mber 参数和 character 参数.n mber 参数指定函数必须返回的字符串的长度.strd p 函数仅使用 character 参数中的第一个字符.character 参数可以是 char 数据类型,string 数据类型或 object 数据类型. 例如: 以下示例使用 strd p 函数返回由重复字符组成的字符串: dim astring as string = "wow! what a string!" dim aobject as new object dim mystring as string aobject = "this is a string that is contained in an object" mystring = strd p(5, "p") ret rns "ppppp" mystring = strd p(10, astring) ret rns "wwwwwwwwww" mystring = strd p(6, aobject) ret rns "tttttt" strreverse strreverse 函数返回一个字符串,该字符串将指定字符串的字符顺序颠倒过来. 例如: dim mystring as string = "abcdefg" dim revstring as string ret rns "gfedcba". revstring = strreverse(mystring) ucase ucase 函数返回一个字符串或字符,包含已转换为大写的指定字符串.只有小写字母被转换为大写字母.所有大写字母和非字母字符均保持不变. 例如: 以下示例使用 ucase 函数返回字符串的大写形式: dim lowercase, uppercase as string lowercase = "hello world 1234" string to convert. uppercase = ucase(lowercase) ret rns "hello world 1234". 使用 system.string 类中的字符串函数 以下是 system 名称空间的 string 类中的字符串函数. 注意:要使用字符串函数,请通过在源代码开始处添加以下代码将 system.string 名称空间导入到项目中: imports system.string compare compare 函数比较输入参数中的两个字符串.通过使用单词排序规则来执行比较.发现不相等情况或比较完两个字符串后,比较将终止. compare 示例: code is not compiled nless it is p t in a s b or in a f nction. dim s1, s2 as string s1 = "testexample" s2 = "testex" msgbox(compare(s2, s1)) ret rns -1. msgbox(compare(s1, s2)) ret rns 1.concat concat 函数将一个或多个字符串相连接,然后返回连接后的字符串. concat 示例: 以下示例显示了如何使用 concat 的重载版本: code is not compiled nless it is p t in a s b or in a f nction. dim s1, s2, sa(3) as string sa(0) = "a" sa(1) = "b" sa(2) = "c" s1 = "test" s2 = "example" s1 = concat(s1, s2) ret rns testexample. msgbox(s1) msgbox(concat(sa)) ret rns abc.copy copy 函数将指定字符串中的值复制到另一个字符串中. copy 示例: code is not compiled nless it is p t in a s b or in a f nction. dim s1, s2 as string s1 = "hello world" copy the string s1 to s2. s2 = copy(s1) msgbox(s2) displays hello world.remove remove 函数从指定字符串的指定位置开始删除指定数目的字符.remove 函数有两个参数.分别是 startindex 参数和 co nt 参数.startindex 参数指定开始删除字符的字符串位置.co nt 参数指定要删除的字符数. remove 示例: code is not compiled nless it is p t in a s b or in a f nction. dim s1, s2 as string s1 = "hello world" removes 3 characters starting from character e. s2 = s1.remove(1, 3) msgbox(s2) displays hello world. s bstring s bstring 函数从指定字符串的指定位置开始检索字符串. s bstring 示例: 以下示例将从指定的字符位置开始并按指定的长度来检索子字符串: code is not compiled nless it is p t in a s b or in a f nction. dim s1, s2 as string s1 = "hello world" s2 = s1.s bstring(6, 5) ret rns world. msgbox(s2)tochararray tochararray 函数将字符串中的字符复制到 unicode 字符数组中. tochararray 示例: 以下示例将指定位置中的字符复制到 character 数组中: code is not compiled nless it is p t in a s b or in a f nction. dim s1 as string dim ch(10) as char s1 = "hello world" copies the characters starting from w to d to a character array. ch = s1.tochararray(6, 5) msgbox(ch(3)) displays l.tolower tolower 函数采用一个字符串作为参数,然后以小写形式返回该字符串的副本. tolower 示例: code is not compiled nless it is p t in a s b or in a f nction. dim s1, s2 as string s1 = "hello world" s2 = s1.tolower converts any ppercase characters to lowercase. msgbox(s2) displays hello world.toupper toupper 函数采用一个字符串作为参数,然后以大写形式返回该字符串的副本. toupper 示例: code is not compiled nless it is p t in a s b or in a f nction. dim s1, s2 as string s1 = "hello world" s2 = s1.toupper converts any lowercase characters to ppercase. msgbox(s2) displays hello world.trim,trimstart 和 trimend 这些函数会返回一个包含指定字符串的副本的字符串.使用 trim 函数时,既没有起始空格也没有尾随空格.使用 trimstart 函数时,没有起始空格.使用 trimend 函数时,没有尾随空格. 例如: 以下示例使用 trimstart 函数删除字符串变量开始处的空格,使用 trimend 函数删除字符串变量末尾的空格,以及使用 trim 函数删除字符串变量中的起始空格和尾随空格: code is not compiled nless it is p t in a s b or in a f nction. dim s1, s2 as string s1 = " hello world " s2 = s1.trim ret rns hello world witho t any white spaces. s2 = s1.trimstart removes the spaces at the start. s2 = s1.trimend removes the white spaces at the end.