C# 关于URL地址操作
// 获取域名(不包括二级域名)
public string GetUrlDomainName(string strHtmlPagePath)
{
string p = @"http://[^\.]*\.(?<domain>[^/]*)";
Regex reg = new Regex(p, RegexOptions.IgnoreCase);
Match m = reg.Match(strHtmlPagePath);
return m.Groups["domain"].Value;
}
public string GetUrlDomainName(string strHtmlPagePath)
{
string p = @"http://[^\.]*\.(?<domain>[^/]*)";
Regex reg = new Regex(p, RegexOptions.IgnoreCase);
Match m = reg.Match(strHtmlPagePath);
return m.Groups["domain"].Value;
}
// 截取域名(包括二级域名)
Uri uriTmp = new Uri(@"http://fff.sohu.com/newsecond/searchmore/bs-CS__address_sdf--1_ff=ffffssssss.htm");
Response.Write(uriTmp.Host.ToString());
Uri uriTmp = new Uri(@"http://fff.sohu.com/newsecond/searchmore/bs-CS__address_sdf--1_ff=ffffssssss.htm");
Response.Write(uriTmp.Host.ToString());
其他操作:
public string[] GetUrlFolerName(string strHtmlPagePath)
{
//抓取网址字符串中的文件目录
int at = 0;
int start = 0;
int notei = 0;
int endi = 0;
int[] myIntArray = new int[10];
string[] ArrayFolderName = null;
string NewFolderName;
while ((start < strHtmlPagePath.Length) && (at > -1))
{
at = strHtmlPagePath.IndexOf('/', start);
if (at == -1) break;
myIntArray[notei] = at;
start = at + 1;
notei = notei + 1;
endi = at;
}
ArrayFolderName = new string[notei - 1];
for (int i = 0; i < notei; i++)
{
if (myIntArray[i] > 0)
{
if (myIntArray[i + 1] > 0)
{
NewFolderName = strHtmlPagePath.Substring(myIntArray[i] + 1, myIntArray[i + 1] - myIntArray[i] - 1);
ArrayFolderName.SetValue(NewFolderName, i);
}
}
}
return ArrayFolderName;
}
public string GetUrlFileName(string strHtmlPagePath)
{
//抓取网址字符串中的文件名称
int at = 0;
int start = 0;
int notei = 0;
int endi = 0;
int[] myIntArray = new int[10];
string NewFileName = "";
while ((start < strHtmlPagePath.Length) && (at > -1))
{
at = strHtmlPagePath.IndexOf('/', start);
if (at == -1) break;
myIntArray[notei] = at;
start = at + 1;
notei = notei + 1;
endi = at;
}
for (int i = 0; i < notei; i++)
{
if (myIntArray[i] > 0)
{
if (myIntArray[i + 1] == 0)
{
NewFileName = strHtmlPagePath.Substring(myIntArray[i] + 1, strHtmlPagePath.Length - myIntArray[i] - 1);
}
}
}
return NewFileName.ToLower();
}
{
//抓取网址字符串中的文件目录
int at = 0;
int start = 0;
int notei = 0;
int endi = 0;
int[] myIntArray = new int[10];
string[] ArrayFolderName = null;
string NewFolderName;
while ((start < strHtmlPagePath.Length) && (at > -1))
{
at = strHtmlPagePath.IndexOf('/', start);
if (at == -1) break;
myIntArray[notei] = at;
start = at + 1;
notei = notei + 1;
endi = at;
}
ArrayFolderName = new string[notei - 1];
for (int i = 0; i < notei; i++)
{
if (myIntArray[i] > 0)
{
if (myIntArray[i + 1] > 0)
{
NewFolderName = strHtmlPagePath.Substring(myIntArray[i] + 1, myIntArray[i + 1] - myIntArray[i] - 1);
ArrayFolderName.SetValue(NewFolderName, i);
}
}
}
return ArrayFolderName;
}
public string GetUrlFileName(string strHtmlPagePath)
{
//抓取网址字符串中的文件名称
int at = 0;
int start = 0;
int notei = 0;
int endi = 0;
int[] myIntArray = new int[10];
string NewFileName = "";
while ((start < strHtmlPagePath.Length) && (at > -1))
{
at = strHtmlPagePath.IndexOf('/', start);
if (at == -1) break;
myIntArray[notei] = at;
start = at + 1;
notei = notei + 1;
endi = at;
}
for (int i = 0; i < notei; i++)
{
if (myIntArray[i] > 0)
{
if (myIntArray[i + 1] == 0)
{
NewFileName = strHtmlPagePath.Substring(myIntArray[i] + 1, strHtmlPagePath.Length - myIntArray[i] - 1);
}
}
}
return NewFileName.ToLower();
}