Skip to content

Instantly share code, notes, and snippets.

@PurwantoGZ
Last active March 17, 2017 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PurwantoGZ/a1f4e5af17c0c0c72e905717023894dd to your computer and use it in GitHub Desktop.
Save PurwantoGZ/a1f4e5af17c0c0c72e905717023894dd to your computer and use it in GitHub Desktop.
Parsing Url in C#
class Program
{
static void Main(string[] args)
{
string url = "nama=purwanto&pacar=cantik & sexy&mobil=subaru & ferari&rumah=besar & mewah&rejeki=lancar & barokah & manfaat&ortu=senang bahagia";
Console.WriteLine("Text Asli\n{0}\n\nCount: {1} item\n",url,ParsingUrl(url).Count);
foreach (var item in ParsingUrl(url))
{
Console.WriteLine("{["+item.Key+"]:["+item.Value+"]}");
}
Console.ReadKey();
}
static Dictionary<string,string> ParsingUrl(string input_stream) {
Dictionary<string, string> result = new Dictionary<string, string>();
List<string> listParse = new List<string>();
string[] result_list = input_stream.Split('&');
List<string> args_params = new List<string>();
for (int i = 0; i < result_list.Length; i++)
{
if (result_list[i].Contains('='))
{
string[] params_split = result_list[i].Split('=');
if (params_split[0] != null)
{
args_params.Add(params_split[0]);
}
}
}
for (int i = 0; i < args_params.Count; i++)
{
string arg = (i == 0 ? "" : "&") + args_params[i] + "=";
int startIndex = input_stream.IndexOf(arg);
if (startIndex >= 0)
{
startIndex += arg.Length;
int endIndex = (i + 1) == args_params.Count ? input_stream.Length : (input_stream.IndexOf("&" + args_params[i + 1]) == -1 ? input_stream.Length : input_stream.IndexOf("&" + args_params[i + 1]));
result.Add(args_params[i], input_stream.Substring(startIndex, endIndex - startIndex));
}
}
return result;
}
}
@PurwantoGZ
Copy link
Author


capture

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment