ASP中字符串函数的应用

ASP中字符串函数的应用

--------------------------------------------
作者:悟者
来源:程序设计联盟
http://www.pgsun.com
EMAIL:[email protected]
欢迎前来学习交流!
--------------------------------------------

1. 字符串函数:

Len(str):取得字符串的长度
Instr(str1,str2):从字符串str1,寻找另一个字符串str2第一个出现的位置
Left(str,n):从字符串str左起取n个字符
Right(str,n):从字符串str右起取n个字符
Mid(str1,n1,n2):从字符串第n1个字符开始,取出n2个字符。

2.字符串函数应用:
1)通过字符函数设计出一个字符串长度控制函数:
防止超过行宽字符串换行:

 1   
 2Function strleft(string,leng)   
 3Dim str1   
 4Dim i,j,k   
 5j = Len(string)   
 6k = 0   
 7For i = 1 To j   
 8str1 = Mid(string,i,1)   
 9If Asc(str1) > 0 Then   
10k = k + 1   
11Else   
12k = k + 2   
13End If   
14If k > leng Then   
15strLeft = Left(string,i) & "..."   
16Exit For   
17Else   
18strLeft = string   
19End If   
20Next   
21End Function   

2)通过字符串函数检测输出是否为电子邮件地址:

 1   
 2Function EMAIL(string)   
 3Dim str1   
 4Dim i,j,k,l   
 5j = Len(string)   
 6k = 0   
 7for i = 1 to j   
 8str1=Mid(string,i,1)   
 9if str1 = "@" then   
10k = k + 1   
11l = i   
12end if   
13next   
14str2 = Mid(string,l+1,1)   
15if (k=1) And ((str2>="a") And (str2<="z")) or ((str2>="A") And (str2<="Z")) then   
16EMAIL = string   
17else   
18EMAIL = "miss!"   
19end if   
20End Function   

此检测函数并不充分,并没有检测".",如有兴趣请补充。

Published At
Categories with Web编程
Tagged with
comments powered by Disqus