using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
public static class AdvancedSearchCriteria
{
public static string MouldSearchCriteria(string searchCriteria)
{
string newSearchCriteriaString = ConvertAllWhiteSpaceToSingleSpace(searchCriteria);
int searchCriteriaSpaceIndex = searchCriteria.IndexOf(" ");
int searchCriteriaQuotesIndex = searchCriteria.IndexOf("\"");
int countForQuotes = CountForQuotes(searchCriteria);
if (countForQuotes > 1)
{
if (countForQuotes % 2 == 0)
{
int cursorPosition = 0;
int quotesLoops = countForQuotes / 2;
List
for (int i = 0; i < quotesLoops; i++)
{
int startQuote = searchCriteria.IndexOf('"', cursorPosition);
int endQuote = searchCriteria.IndexOf('"', startQuote + 1);
cursorPosition = endQuote + 1;
string t = GetTextWithinQuotes(startQuote + 1, (endQuote - startQuote) - 1, searchCriteria);
quoteTextList.Add(t);
}
string t1 = CreateSearchTextForQuoteValues(quoteTextList);
string t2 = CreateSearchTextForNonQuoteValues(quoteTextList, searchCriteria);
if (t2.Equals(""))
newSearchCriteriaString = t1;
else
newSearchCriteriaString = t1 + " and " + t2;
}
else
{
int quoteLastIndex = searchCriteria.LastIndexOf("\"");
searchCriteria = searchCriteria.Remove(quoteLastIndex, 1);
newSearchCriteriaString = MouldSearchCriteria(searchCriteria);
}
}
else
{
if (searchCriteriaSpaceIndex == -1 && searchCriteriaQuotesIndex == -1)
newSearchCriteriaString = AddQuotes(searchCriteria);
if (searchCriteriaSpaceIndex == -1 && searchCriteriaQuotesIndex >= 0)
newSearchCriteriaString = AddQuotes(searchCriteria.Replace("\"", ""));
if (searchCriteriaSpaceIndex >= 0)
newSearchCriteriaString = GetSpaceSplit(searchCriteria);
}
return newSearchCriteriaString;
}
public static string CreateSearchTextForQuoteValues(List
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < quoteTextList.Count; i++)
{
sb.Append(AddQuotes(quoteTextList[i]));
if (i != quoteTextList.Count - 1)
sb.Append(" and ");
}
return (sb.ToString());
}
public static string CreateSearchTextForNonQuoteValues(List
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < quoteTextList.Count; i++)
{
searchString = searchString.Replace(AddQuotes(quoteTextList[i]), "");
}
searchString = ConvertAllWhiteSpaceToSingleSpace(searchString);
if (searchString.StartsWith(" "))
searchString = searchString.Substring(1);
if (searchString.EndsWith(" "))
searchString = searchString.Substring(0, searchString.Length - 1);
searchString = GetSpaceSplit(searchString);
if (searchString.Equals("\"\""))
searchString = "";
return (searchString);
}
public static string ConvertAllWhiteSpaceToSingleSpace(string searchString)
{
return System.Text.RegularExpressions.Regex.Replace(searchString, @"\s+", " ");
}
public static string GetTextWithinQuotes(int startQuote, int endQuote, string searchString)
{
string fortest = searchString.Substring(startQuote, endQuote);
return fortest;
}
public static string AddQuotes(string searchString)
{
searchString = "\"" + searchString + "\"";
return searchString;
}
public static string GetSpaceSplit(string searchString)
{
string[] splitWithSpace = searchString.Split(' ');
StringBuilder sb = new StringBuilder();
for (int i = 0; i < splitWithSpace.Length; i++)
{
sb.Append(AddQuotes(splitWithSpace[i]));
if (i != splitWithSpace.Length - 1)
sb.Append(" and ");
}
return (sb.ToString());
}
public static int CountForQuotes(string searchString)
{
char quotes = '"';
int count = 0;
foreach (char c in searchString)
{
if (c.Equals(quotes))
count++;
}
return count;
}
}