我想到了这一点,一个办法要做到这一点是为程序定义一些分隔符以知道在哪里破坏URL。
在我们这样做之前,我们需要先替换特殊字符。
让我们开始:
string searchFor;
string replaceWith;
static void Main(string[] args)
{
// First we need to replace the special characters:
ReplaceSubstrings replace = new ReplaceSubstrings();
string s = "http://Mydatabase-live/ReportServer?%2fADMIN%2fSTATS+-+SCHEDULE&TEAMNM=2015 TERRIER JV&rs:Command=Render&rs:Format=PDF";
// We need to replace:
// "%2f" with "/"
// "+-+" with "-"
// using System.Text.RegularExpressions
replace.searchFor = "%2f";
replace.replaceWith = "/";
s = Regex.Replace(s, replace.searchFor);
replace.searchFor = "+-+";
replace.replaceWith = "-";
s = Regex.Replace(s, replace.searchFor);
// Your URL will now look like this:
Console.WriteLine(s);
// Output: http://Mydatabase-live/ReportServer?/ADMIN/STATS-SCHEDULE&TEAMNM=2015 TERRIER JV&rs:Command=Render&rs:Format=PDF
// Add the delimiters
char[] delimiters = {'?', '&', '='};
string[] words = s.Split(delimiters);
foreach (string s in words)
{
System.Console.WriteLine(s);
}
// Output:
// http://Mydatabase-live/ReportServer
// /ADMIN/STATS-SCHEDULE
// TEAMNM
// 2015 TERRIER JV
// rs:Command
// Render
// rs:Format
// PDF
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
您的网址将在更多的地方比你指定的分离,但是这是你应该怎么做。您可以从第一个=
标志所在的字符串中删除最后一部分,然后执行字符串分隔。
我希望这对你有所帮助。
什么是ReplaceSubstrings replace = new ReplaceSubstrings(); –
它用于创建对象并调用构造函数。它等同于:'Class1 obj = new Class1();' – Krayen