您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页使用vbscript的字符串操作函数处理文本

使用vbscript的字符串操作函数处理文本

来源:二三娱乐
使用vbscript的字符串操作函数处理文本

我们可以使用vbscript或者asp支持的其他脚本在服务器端对获取的信息进行整理。这些函数都是很常见的,现在把它们拿出来,好像也没有什么用。 1、instr函数 Instr()

FUNCTION: 返回字符或字符串在另一个字符串中第一次出现的位置. SYNTAX: Instr([start, > strToBeSearched, strSearchFor [, compare>)

ARGUMENTS: Start为搜索的起始值,strToBeSearched接受搜索的字符串 strSearchFor要搜索的字符compare 比较方式(详细见ASP常数) EXAMPLE: <%

strText = \"This is a test!!\" pos = Instr(strText, \"a\") response.write pos %>

RESULT: 9 2、instrrev函数 InstrRev()

FUNCTION: 同上,只是从字符串的最后一个搜索起

SYNTAX: InstrRev([start, > strToBeSearched, strSearchFor [, compare>) ARGUMENTS: 同上. EXAMPLE: <%

strText = \"This is a test!!\" pos = InstrRev(strText, \"s\") response.write pos %>

RESULT: 13 3、mid函数 Mid()

FUNCTION: 返回特定长度的字符串(从start开始,长度为length). SYNTAX: Mid(string, start [, length>) ARGUMENTS: EXAMPLE: <%

strTest = \"This is a test! Today is Monday.\" response.write Mid(strTest, 17, 5) %>

RESULT: Today 4、replace Replace()

FUNCTION: Returns a string in which a specified sub- string has been replaced with another substring a specified number of times.

SYNTAX: Replace(strToBeSearched, strSearchFor, strReplaceWith [, start [, count [,

compare>>>)

ARGUMENTS: strToBeSearched is a string expression containing a sub-string to be replaced; strSearchFor is the string expression to search for within strToBeSearched; strReplaceWith is the string expression to replace sub-string strSearchFor; start (optional) is the numeric character position to begin search; count (optional) is a value indicating the comparision constant. EXAMPLE: <%

strTest = \"This is an apple!\"

response.write Replace(strTest, \"apple\ %>

RESULT: This is an orange!

以上的四个函数是在远程获取中用的最多的,比起正则表达式,虽然功能上不足,但是使用他们显得更加简易。 另外一些可能用到的 trim()

FUNCTION: 去掉字符串两边的空格. SYNTAX: Rtrim(string) ARGUMENTS: EXAMPLE: <%

strTest = \" This is a test!! \" response.write RTrim(strTest) %>

RESULT: This is a test!!

还有ltrim和rtrim,分别是去掉字符串左边和右边的空格。StrReverse() FUNCTION: 反排一字符串 SYNTAX: StrReverse(string) ARGUMENTS: EXAMPLE: <%

strTest = \"This is a test!!\"

response.write StrReverse(strTest) %>

RESULT: !!tset a si sihT

例子是落伍者论坛 mofei的文章。

天气预报 - 默飞冲天 <%

'制作:默飞 'QQ:33224360

'http://mofei.xinxiu.com

'天气预报最新适用采集程序

'声明取得目标信息的函数,通过XML组件进行实现。 Function GetURL(url)

Set Retrieval = CreateObject(\"Microsoft.XMLHTTP\") With Retrieval

.Open \"GET\.Send

GetURL = bytes2bstr(.responsebody)

'对取得信息进行验证,如果信息长度小于100则说明截取失败 if len(.responsebody)<100 then

response.write \"获取远程文件 \"&url&\" 失败。\" response.end end if End With

Set Retrieval = Nothing End Function

' 二进制转字符串,否则会出现乱码的! function bytes2bstr(vin) strreturn = \"\"

for i = 1 to lenb(vin)

thischarcode = ascb(midb(vin,i,1)) if thischarcode < &h80 then

strreturn = strreturn & chr(thischarcode) else

nextcharcode = ascb(midb(vin,i+1,1))

strreturn = strreturn & chr(clng(thischarcode) * &h100 + cint(nextcharcode)) i = i + 1 end if next

bytes2bstr = strreturn end function

'声明截取的格式,从Start开始截取,到Last为结束 Function GetKey(HTML,Start,Last) filearray=split(HTML,Start)

filearray2=split(filearray(1),Last) GetKey=filearray2(0) End Function

Dim Softid,Url,Html,Title

'把下面的[将乐]改成你的城市,就是你城市的天气预报了,可到www.tq121.com.cn里查一下,有没收录你的城市,一般到县级的都有。

Url=\"http://weather.tq121.com.cn/detail.php?city=将乐\" Html = GetURL(Url)

'搜索要偷取的内容的开始位置

Str_start = instr(Html, \"预报发布时间\")

Str_start = instr(Str_start, Html, \"

\") '搜索要偷取的内容的结束位置

Str_end = instr(Str_start, Html, \"

\") '截取内容

Html = Mid(Html, Str_start, Str_end - Str_start) '调整表格

Html=replace(replace(Html,\"width=\"\"979\"\"\大表格 Html=replace(replace(Html,\"width=\"\"133\"\"\背景大小 Html=replace(replace(Html,\"width=\"\"973\"\"\

Html=replace(replace(Html,\"width=\"\"113\"\"\日期表格 Html=replace(replace(Html,\"width=\"\"57\"\"\图片表格 Html=replace(replace(Html,\"width=\"\"50\"\"\Html=replace(replace(Html,\"height=\"\"57\"\"\Html=replace(Html,\"width=\"\"141\"\"\表格分配 Html=replace(Html,\"width=\"\"140\"\"\Html=replace(Html,\"width=\"\"139\"\"\Html=replace(Html,\"width=\"\"133\"\"\Html=replace(Html,\"width=\"\"125\"\"\Html=replace(Html,\"width=\"\"107\"\"\

Html=replace(replace(Html,\"../images/\图片路径 response.write Html

'Response.Write\"采集完毕!\" %>

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务