QueryBuilder: A Simple Way to Read and Write Query Strings
2015-02-12 16:08
316 查看
QueryBuilder: A Simple Way to Read and Write Query Strings
http://www.codeproject.com/Articles/875164/QueryBuilder-A-Simple-Way-to-Read-and-Write-Query代码下载地址:http://pan.baidu.com/s/1ntuWEpV
namespace TestApplication.Query { public class QueryParameter { public string Name { get; set; } public string Value { get; set; } internal bool IsFirst { get; set; } public QueryParameter(string name, string value) { this.Name = name; this.Value = value; } public QueryParameter(string name) : this(name, "") { } public static QueryParameter Create(string parameter) { string trimmed = parameter.Replace("&", "").Replace("?", ""); if (trimmed.Contains("=")) { string[] split = trimmed.Split('='); return new QueryParameter(split[0], split[1]); } return new QueryParameter(trimmed); } private string Flatten() { return (this.IsFirst ? '?' : '&') + this.Name + (string.IsNullOrEmpty(this.Value) ? "" : "=") + this.Value; } public override string ToString() { return this.Flatten(); } //... } }
using System.Collections.Generic; using System.Linq; namespace TestApplication.Query { public class QueryBuilder { private List<QueryParameter> _parameters; public QueryBuilder() { _parameters = new List<QueryParameter>(); } public QueryParameter this[int index] { get { return _parameters[index]; } } public string this[string name] { get { return HasParameter(name) ? _parameters.Find(x => x.Name == name).Value : null; } } public bool HasParameter(string name) { return _parameters.Exists(p => p.Name == name); } #region method public void AddParameter(QueryParameter parameter) { _parameters.Add(parameter); _parameters.Last().IsFirst = (_parameters.Count == 1); } public void AddParameter(string name, string value) { AddParameter(new QueryParameter(name, value)); } public void RemoveParameter(QueryParameter parameter) { _parameters.Remove(parameter); } public void RemoveParameter(string name) { QueryParameter param = _parameters.Find(x => x.Name == name); _parameters.Remove(param); } public static QueryBuilder Create(string queryString) { QueryBuilder toReturn = new QueryBuilder(); if (queryString.Contains('&')) { string[] arr = queryString.Split('&'); foreach (string p in arr) { toReturn.AddParameter(QueryParameter.Create(p)); } } else { toReturn.AddParameter(QueryParameter.Create(queryString)); } return toReturn; } private string CompileQueryString() { string toReturn = ""; foreach (QueryParameter p in _parameters) { toReturn += p.ToString(); } return toReturn; } public override string ToString() { return CompileQueryString(); } #endregion //... } }//测试下
class Program { static void Main(string[] args) { Query.QueryBuilder querybuilder = new Query.QueryBuilder(); querybuilder.AddParameter("name", "jon"); querybuilder.AddParameter("bb", "123"); querybuilder.AddParameter("aa", "mike"); querybuilder.AddParameter("cc", "hello"); Console.WriteLine(querybuilder.ToString()); Console.ReadKey(); } //.. }
?name=jon&bb=123&aa=mike&cc=hello
相关文章推荐
- How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
- How To Read and Write XML Documents with GDataXML
- How to mount partition with ntfs file system and read write access
- (转载)XML Tutorial for iOS: How To Read and Write XML Documents with GDataXML
- Spring Batch Read from DB and Write to File
- A Simple Way to Enable a Windows XP Look and Feel for VC++ 6.0 MFC Applications
- Read and Write compressed data to a binary file using ASP.NET
- write to and read from files (读写文件)
- JSON.simple example – Read and write JSON
- 笔试题:Suggest a simple and fast way to multiply the input integer digit by 7。
- How To Read and Write XML Documents with GDataXML
- Simple and easiest way to make a screen shot in .net 2.0
- Read and write SD cards in a raw way with dd
- a simple and easy way to export grid(grid view) to excel
- CSpreadSheet - A Class to Read and Write to Excel and Text Delimited Spreadsheet
- How to use java Properties API -- read and write
- Read and Write to a Keyboard device in Linux using C++
- Java Excel API - A Java API to read, write and modify Excel(收集)
- Java libraries to read and write PDF files
- Read a Text File with VBA in Excel, and Write the Text to a Spreadsheet