Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public string TrimEmptyQueries(string queryString)
- {
- var result = Regex.Replace(queryString, @"(?<=\&|\?)([^\&\n\=]+|[^\&\n]+\=)(?>&|$)", "")
- .Trim('&');
- return result == "?" ? "" : result;
- }
- public void Main()
- {
- var testData = new[]
- {
- new TestData("?foo=foo&bar&foobar&blah=blah&bleh&hi&bye&asdf&", "?foo=foo&blah=blah"),
- new TestData("?foo=foo&bar=&foobar=foobar", "?foo=foo&foobar=foobar"),
- new TestData("?foo=foo&bar&foobar=foobar", "?foo=foo&foobar=foobar"),
- new TestData("?foo=foo&bar=bar", "?foo=foo&bar=bar"),
- new TestData("?foo=foo", "?foo=foo"),
- new TestData("?foo=foo&bar", "?foo=foo"),
- new TestData("?foo=foo&bar=", "?foo=foo"),
- new TestData("?bar&foo=foo", "?foo=foo"),
- new TestData("?bar=&foo=foo", "?foo=foo"),
- new TestData("?foo=", ""),
- new TestData("?foo", "")
- };
- foreach (var test in testData)
- {
- var result = TrimEmptyQueries(test.InitialValue);
- Console.WriteLine("===============================================================================");
- Console.WriteLine("'{0}' {1}", test.InitialValue, " should be");
- Console.WriteLine("'{0}' {1}", test.ExpectedValue, " and is");
- Console.WriteLine("'{0}'", result);
- Console.WriteLine(result == test.ExpectedValue ? "Passed" : "Failed");
- }
- }
- public class TestData
- {
- public string InitialValue { get; set; }
- public string ExpectedValue { get; set; }
- public TestData(string initialValue, string expectedValue)
- {
- InitialValue = initialValue;
- ExpectedValue = expectedValue;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement