Parse a search string

  • Thread starter Thread starter Mufasa
  • Start date Start date
M

Mufasa

Does anybody have any code that will take a search string and parse it in to
the appropriate parts?

For instance: +google -news -bush

TIA - J.
 
Does anybody have any code that will take a search string and parse it
in to
the appropriate parts?

For instance: +google -news -bush

It's a very vague question. But, assuming you've got a string and you
want it split into parts where spaces (or any specific character) are the
separator: String.Split().

Pete
 
Mufasa said:
Does anybody have any code that will take a search string and parse it in to
the appropriate parts?

For instance: +google -news -bush

For inspiration see below.

Arne

==========================================================

public static void ParseSplit(string s)
{
string[] parts = s.Split(" ".ToCharArray());
foreach(string part in parts)
{
if(part[0] == '+')
{
Console.WriteLine("Include : " + part.Substring(1));
}
else if(part[0] == '-')
{
Console.WriteLine("Exclude : " + part.Substring(1));
}
}
}
private static readonly Regex incl = new Regex(@"(?:\+)(\w+)(?: |$)",
RegexOptions.Compiled);
private static readonly Regex excl = new Regex(@"(?:\-)(\w+)(?: |$)",
RegexOptions.Compiled);
public static void ParseRegex(string s)
{
foreach(Match m in incl.Matches(s))
{
Console.WriteLine("Include : " + m.Groups[1].Value);
}
foreach(Match m in excl.Matches(s))
{
Console.WriteLine("Exclude : " + m.Groups[1].Value);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top