Code
/* ----------------------------------------
* Function: C # operation URL parameters
* Author: robot. h
* Created: 20081117
--------------------------------------- */
Using System;
Using System. Text. regularexpressions;
Using System. runtime. remoting. contexts;
Class App
{
Static Void Main ()
{
// Generate random
Random Rand = New Random (environment. tickcount );
Int Num = Rand. Next ( 1 , 10000 );
/*
* Http://news.sina.com.cn
* Http://news.sina.com.cn ? Page = 1
* Http://news.sina.com.cn ? Page = 1 & TS = 1341 &&
* Http://news.sina.com.cn ? TS = 14321 & page = 1? &
* Http://news.sina.com.cn ? U = 2 & TS = 14321 & page = 1 &&
*/
String URL = " Http://news.sina.com.cn? U = 2 & TS = 14321 & page = 1 " ;
String Paramtext = " TS " ;
String Paramvalue = Num. tostring ();
Console. writeline (buildurl (URL, paramtext, paramvalue ));
Console. Read ();
}
// If there is a key value in the URL, replace it with the value. If there is no key, append it.
Public Static String Buildurl ( String URL, String Paramtext, String Paramvalue)
{
RegEx Reg = New RegEx ( String . Format ( " {0} = [^ &] * " , Paramtext), regexoptions. ignorecase );
RegEx reg1 = New RegEx ( " [&] {2 ,} " , Regexoptions. ignorecase );
String _ URL = Reg. Replace (URL, "" );
// _ Url = reg1.replace (_ URL ,"");
If (_ URL. indexof ( " ? " ) = - 1 )
_ URL + = String . Format ( " ? {0} = {1} " , Paramtext, paramvalue ); // ?
Else
_ URL + = String . Format ( " & {0 }={ 1} " , Paramtext, paramvalue ); // &
_ URL = Reg1.replace (_ URL, " & " );
_ URL = _ URL. Replace ( " ? & " , " ? " );
Return _ URL;
}