移除Url参数的函数

随手写了一两个移除Url参数的函数,怕以后找不到了。(未经过严格测试,因此暂时发现用不着了,呵呵)
ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// 移除Url中的参数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="url">原始url</param>
InBlock.gif        
/// <param name="parameters">需要移除的参数</param>
ExpandedBlockEnd.gif        
/// <returns>移除了参数的Url</returns>

None.gif          private   string  RemoveUrlParameters( string  url ,  params   string [] parameters)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string cleanUrl = string.Empty;
InBlock.gif            
string parasString = string.Empty;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] urlArray = new string[]dot.gif{};
InBlock.gif            
string resultUrl = string.Empty;
InBlock.gif
InBlock.gif            
//将原始Url以“?”“&”分解成字符串数组(因为参数是以此二符号作为分隔符的)
InBlock.gif
            if (url != string.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                urlArray 
= url.ToLower().Split(new string[] dot.gif"?","&" }, StringSplitOptions.RemoveEmptyEntries);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//其中数组的第一个元素是无任何参数的Url
InBlock.gif
            cleanUrl = urlArray[0];
InBlock.gif            resultUrl 
+= cleanUrl;
InBlock.gif
InBlock.gif            
//数组的维数
InBlock.gif
            int countUrlArray = urlArray.Length;
InBlock.gif            
//由于用于隔离参数和Url之间需要使用“?”,而参数之间的分隔则使用"&",而且只有第一次的时候是用“?”
InBlock.gif
            int addTimes = 0;
InBlock.gif
InBlock.gif            
//判断所有要移除的参数和现有Url字符串数组之间的关系,
InBlock.gif            
//如果当前遍历到的Url字符串
InBlock.gif
            foreach (string para in parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for(int i =1;i<countUrlArray ;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (!ParameterContains(urlArray[i],para))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if (addTimes == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            resultUrl 
= resultUrl + "?" + urlArray[i];
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            resultUrl 
= resultUrl + "&" + urlArray[i];
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        addTimes
++;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return resultUrl;
ExpandedBlockEnd.gif        }

None.gif
ExpandedBlockStart.gifContractedBlock.gif        
/**/ /// <summary>
InBlock.gif        
/// 判断形如“abcdefg=1234567”的字符串等号左侧是否是参数parameter,是则返回true,否则返回false
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="paraString">形如“abcdefg=1234567”的字符串</param>
InBlock.gif        
/// <param name="parameter"></param>
ExpandedBlockEnd.gif        
/// <returns></returns>

None.gif          private   bool  ParameterContains( string  paraString,  string  parameter)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
bool isContain = false;
InBlock.gif            
if(paraString!=string.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
string[] paraKey = paraString.Trim().Split(new char[] dot.gif'=' });
InBlock.gif                
if (paraKey[0== parameter)
InBlock.gif                    isContain 
= true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return isContain;
ExpandedBlockEnd.gif        }

None.gif
ExpandedBlockStart.gifContractedBlock.gif        
/**/ /// <summary>
InBlock.gif        
/// 移除Url中的全部参数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="url">原始Url</param>
ExpandedBlockEnd.gif        
/// <returns></returns>

None.gif          private   string  RemoveUrlParameters( string  url)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string cleanUrl = string.Empty;
InBlock.gif            
if (url != string.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
string[] urlArray = url.ToLower().Split(new string[] dot.gif"?" }, StringSplitOptions.RemoveEmptyEntries);
InBlock.gif                cleanUrl 
= urlArray[0];
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return cleanUrl;
ExpandedBlockEnd.gif        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值