CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class Program
{
public static void Main()
{
Console.WriteLine(RemoveEmptyUrlParameters("one=aa&two=&three=aaa&four="));
}
public static string RemoveEmptyUrlParameters(string input)
{
var results = HttpUtility.ParseQueryString(input);
Dictionary<string, string> nonEmpty = new Dictionary<string, string>();
foreach(var k in results.AllKeys)
{
if(!string.IsNullOrWhiteSpace(results[k]))
{
nonEmpty.Add(k, results[k]);
}
}
return string.Join("&", nonEmpty.Select(kvp => $"{kvp.Key}={kvp.Value}"));
}
}