asp截取url中的字符

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了asp截取url中的字符相关的知识,希望对你有一定的参考价值。

问题难度比较大,完成后可追加分数
要求用asp来实现
有一个url
例如http://www.baidu.com/321/23/AA775/432/BB889/33/CC993/38cs/ek3
asp中的变量有
aa
bb
cc
要求使
aa等于url中/AA775/中AA后面的数字的775
bb等于url中/432/BB889/33/中BB后面数字的的889
cc等于url中33/CC993/38cs中CC后面数字的的993

难度比较大,谢谢帮忙
非常感谢,还有个问题,
例如url地址是/sdfsdf/sdfsf/CC933.html
如何使cc等于sdfsf/CC933.html中的933而不要.html呢,也就是只要数字

using System;
using System.Net;
using System.IO;

/// <summary>
/// DataCollection 的摘要说明
/// </summary>
public class DataCollection

public string HTML = "";//成员变量,保存HTML代码

public DataCollection()




/// <summary>
/// 设定指定URL页面代码
/// </summary>
/// <param name="vUrl">指定URL</param>
/// <returns>返回成功信息</returns>
public int setHtmlString(string vUrl)

int iRet = 0;
try

WebRequest wreq = WebRequest.Create(vUrl);

HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse();

Stream s = wresp.GetResponseStream();

StreamReader objReader = new StreamReader(s, System.Text.Encoding.GetEncoding("GB2312"));

string sLine = "";
int i = 0;

while (sLine != null)

i++;
sLine = objReader.ReadLine();
if (sLine != null)

HTML += sLine;



int c = HTML.Length;
iRet = 1;

catch(Exception ex)

iRet = -1;


return iRet;


/// <summary>
/// 设定指定页面代码(重载)
/// </summary>
/// <param name="content">指定页面代码</param>
/// <param name="i"></param>
public void setHtmlString(string content,int i)

HTML = content;


/// <summary>
/// 返回指定标签在指定页面代码的索引
/// </summary>
/// <param name="vHtml">指定页面代码</param>
/// <param name="vSign">指定标签</param>
/// <returns>返回索引</returns>
public int[] getSignIndex(string vHtml, string vSign)

int sum = 0;
for (int i = 0; i < vHtml.Length - vSign.Length; i++)

//if (i == 22715)
//
// string c = vHtml.Substring(i, vSign.Length);
//
if (vSign.Equals(vHtml.Substring(i, vSign.Length)))

sum++;


int[] index = new int[sum];
sum = 0;
for (int i = 0; i < vHtml.Length - vSign.Length; i++)


if (vSign.Equals(vHtml.Substring(i, vSign.Length)))

index[sum] = i + vSign.Length;
sum++;



return index;


/// <summary>
/// 返回指定标签在指定页面代码的索引(重载)
/// </summary>
/// <param name="vHtml">指定页面代码</param>
/// <param name="vSign">指定标签</param>
/// <param name="startIndex">指定开始索引</param>
/// <returns>返回索引</returns>
public int[] getSignIndex(string vHtml, string vSign, int[] startIndex)

int[] index = new int[startIndex.Length];

for (int k = 0; k < startIndex.Length; k++)

int sum = 0;
for (int i = startIndex[k]; i < vHtml.Length - vSign.Length; i++)

if (vSign.Equals(vHtml.Substring(i, vSign.Length)))

if (sum == 0)

index[k] = i;

sum++;



return index;


/// <summary>
/// 删除指定的标签
/// </summary>
/// <param name="content">指定内容</param>
/// <param name="sign">指定标签</param>
/// <returns>返回删除后的内容</returns>
public string[] deleteSign(string[] content,string sign)

for (int i = 0; i < content.Length; i++)

content[i] = content[i].Replace(sign, "");

return content;


/// <summary>
/// 通过指定开始结束标签得到中间的字符串
/// </summary>
/// <param name="startSign">开始标签</param>
/// <param name="endSign">结束标签</param>
/// <returns>返回字符串</returns>
public string[] getString(string startSign, string endSign)

int[] start = getSignIndex(HTML, startSign);
int[] end = getSignIndex(HTML,endSign, start);

string[] msg = new string[start.Length];
for (int i = 0; i < msg.Length; i++)

msg[i] = HTML.Substring(start[i], end[i] - start[i]);

return msg;
参考技术A 一楼的回答是可以解决问题,不过.如果URL中没有/AA,是/aa,或者之前还有/AA(如:/AA/AA775)的出现就要进行大量的判断.有点复杂

但是如果用正则表达式的话就简便多了

代码:

url="http://www.baidu.com/321/23/AA775/432/Bx889/33/CC993/38cs/ek3"
aa=RegExpSub("/aa(\d+)",url)
bb=RegExpSub("/bb(\d+)",url)
cc=RegExpSub("/cc(\d+)",url)

'正则表达式匹配,返回首次匹配的第一分组
Function RegExpSub(patrn, str)
Dim regEx
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True
regEx.Global = True
if regEx.test(str) then RegExpSub=regEx.Execute(str)(0).SubMatches.item(0)
End Function本回答被提问者采纳
参考技术B url = "http://www.baidu.com/321/23/AA775/432/BB889/33/CC993/38cs/ek3"
aa = split(url,"/AA")(1) '先将/AA后面的字符串放入变量aa中
aa = split(aa,"/")(0) '再将分割好的字符串进行再次分割

bb = split(url,"/BB")(1)
bb = split(bb,"/")(0)

cc = split(url,"/CC")(1)
cc = split(cc,"/")(0)

php获取url字符串截取路径的文件名和扩展名

<?php
//获取连接里边的id
$url = ‘http://www.rong123.com/cjbkscbsd/x_dfsdfs/24454_1_1.html‘;
function getIdByUrl($url) {
	$id = ‘‘;
	$filename = basename($url,".html");
	$id = str_replace(‘-‘, ‘_‘, $filename); 
	if(strstr($id, ‘_‘)) {
		$conids = explode(‘_‘, $id); 
		$id = $conids[0];
	}
	return $id;
}
echo getIdByUrl($url).‘<hr/>‘;

//获取完整文件名
echo basename($url).‘<hr/>‘;

//获取没有后缀的文件名
echo basename($url,".html");

?>

  

  

以上是关于asp截取url中的字符的主要内容,如果未能解决你的问题,请参考以下文章

请大家推荐asp截取字符 怎样过滤掉图片 只显示文本

请大家推荐asp截取字符 怎样过滤掉图片 只显示文本

js url 字符串截取 转换中文

截取url中的某个字符串后面的值

JavaScript正则表达式--非贪婪模式截取任意字符串的笔记

c语言截取下载url中文件名称