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;
    }

 

//  截取域名(包括二级域名)
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();

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值