zoukankan
html css js c++ java
用 C# 分析 URL 中的参数信息
using
System;
using
System.Collections.Specialized;
using
System.Text.RegularExpressions;
namespace
UrlParse
{
/**/
///
<summary>
///
分析 url 字符串中的参数信息
///
</summary>
class
Class1
{
/**/
///
<summary>
///
应用程序的主入口点。
///
</summary>
[STAThread]
static
void
Main(
string
[] args)
{
string
url
=
"
http://www.xxx.com/xyz/hello.asp?a=3&typeId=45&cc=ILoveYou
"
;
NameValueCollection nvc;
string
baseUrl;
ParseUrl(url,
out
baseUrl,
out
nvc);
//
output results
Console.WriteLine(
"
baseUrl: {0}
"
, baseUrl);
Console.WriteLine(
"
parameters:
"
);
for
(
int
i
=
0
; i
<
nvc.Count; i
++
)
Console.WriteLine(
"
{0}, {1}
"
, nvc.Keys[i], nvc[i]);
Console.ReadLine();
}
/**/
///
<summary>
///
分析 url 字符串中的参数信息
///
</summary>
///
<param name="url">
输入的 URL
</param>
///
<param name="baseUrl">
输出 URL 的基础部分
</param>
///
<param name="nvc">
输出分析后得到的 (参数名,参数值) 的集合
</param>
///
<author>
木野狐(Neil Chen)
</author>
///
<date>
2005-06-23
</date>
static
void
ParseUrl(
string
url,
out
string
baseUrl,
out
NameValueCollection nvc)
{
if
(url
==
null
)
throw
new
ArgumentNullException(
"
url
"
);
nvc
=
new
NameValueCollection();
baseUrl
=
""
;
if
(url
==
""
)
return
;
int
questionMarkIndex
=
url.IndexOf(
'
?
'
);
if
(questionMarkIndex
==
-
1
)
{
baseUrl
=
url;
return
;
}
baseUrl
=
url.Substring(
0
, questionMarkIndex);
if
(questionMarkIndex
==
url.Length
-
1
)
return
;
string
ps
=
url.Substring(questionMarkIndex
+
1
);
//
开始分析参数对
Regex re
=
new
Regex(
@"
(^|&)?(\w+)=([^&]+)(&|$)?
"
, RegexOptions.Compiled);
MatchCollection mc
=
re.Matches(ps);
foreach
(Match m
in
mc)
{
nvc.Add(m.Result(
"
$2
"
), m.Result(
"
$3
"
));
}
}
}
}
查看全文
相关阅读:
Balanced Binary Tree
Minimum Depth of Binary Tree
Path Sum
Flatten Binary Tree to Linked List
Distinct Subsequences
Chp3: Stacks and Queue
Chp2: Linked List
Populating Next Right Pointers in Each Node
Valid Palindrome
Binary Tree Maximum Path Sum
原文地址:https://www.cnblogs.com/RChen/p/179627.html
最新文章
Largest Rectangle in Histogram
Scramble String
Maximal Rectangle
Partition List
Multiply Strings
Gray Code
Decode Ways
Unique Binary Search Trees II
Unique Binary Search Trees
Interleaving String
热门文章
线索二叉树Threaded binary tree
Recover Binary Search Tree
Symmetric Tree
Triangle
常见的设计模式
HashMap与Hashtable的区别
最大子矩阵和
Binary Tree Level Order Traversal II
外排序
Convert Sorted List to Binary Search Tree
Copyright © 2011-2022 走看看