Answered by:
Parsing out "and" and "or" from a string.

Question
-
User-548542357 posted
I am making a search page and would like to parse out "and" and "or" so I can build the SQL query. Basically in the search bar the user can search "dog or cat". I would then parse out the or and basically get 2 strings of 1.dog 2.cat and know to use the OR in the SQL statement. Whats the best way to go about this.
A code example would be nice as well.
Thanks in advance.
Wednesday, September 30, 2009 1:01 PM
Answers
-
User-952121411 posted
Here is a code example of how to do what you need; it works well for me:
Dim SearchString As String = "dog or cat" Dim IsOrOperator, IsAndOperator As Boolean '1st lets figure out if we are going to use an 'Or' or an 'And' for our SQL. 'If 'IndexOf' returns a value <> -1 that indicates it was found. If SearchString.IndexOf("or", StringComparison.OrdinalIgnoreCase) <> -1 Then 'Remove the operator and replace with a 'comma' to use to split the string below SearchString = SearchString.Replace(" or ", ",") IsOrOperator = True ElseIf SearchString.ToLower.IndexOf("and", StringComparison.OrdinalIgnoreCase) <> -1 Then 'Remove the operator and replace with a 'comma' to use to split the string below SearchString = SearchString.Replace(" and ", ",") IsAndOperator = True End If 'Next split the string into individual keywords and place in a String array Dim KeywordsList As String() KeywordsList = SearchString.Split(",") '...later on here is how to loop back through that array made above Dim SearchWord As String = String.Empty For Each Keyword As String In KeywordsList SearchWord = Keyword Next
Hope this helps!
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, October 1, 2009 12:07 PM
All replies
-
User-837620913 posted
My VB is a little rusty, but you can probably figure out the pseudocode:
Dim search as string = "dog or cat"
search.Replace( "or", "") 'replace or with empty string
search.Replace( "and", "" ) 'replace and with empty string
Dim searchTerms() as string() = search.Split( " " ) 'split the string based on the space character
'Note that you might have a couple spaces in the searchTerms array
Wednesday, September 30, 2009 1:14 PM -
User-548542357 posted
ok so im thinking of using contains to find the " or " and " and " so i kwnow how to build the SQL statement. Then use the split function to get the individual terms. Sound idea?
Wednesday, September 30, 2009 1:46 PM -
User-952121411 posted
Here is a code example of how to do what you need; it works well for me:
Dim SearchString As String = "dog or cat" Dim IsOrOperator, IsAndOperator As Boolean '1st lets figure out if we are going to use an 'Or' or an 'And' for our SQL. 'If 'IndexOf' returns a value <> -1 that indicates it was found. If SearchString.IndexOf("or", StringComparison.OrdinalIgnoreCase) <> -1 Then 'Remove the operator and replace with a 'comma' to use to split the string below SearchString = SearchString.Replace(" or ", ",") IsOrOperator = True ElseIf SearchString.ToLower.IndexOf("and", StringComparison.OrdinalIgnoreCase) <> -1 Then 'Remove the operator and replace with a 'comma' to use to split the string below SearchString = SearchString.Replace(" and ", ",") IsAndOperator = True End If 'Next split the string into individual keywords and place in a String array Dim KeywordsList As String() KeywordsList = SearchString.Split(",") '...later on here is how to loop back through that array made above Dim SearchWord As String = String.Empty For Each Keyword As String In KeywordsList SearchWord = Keyword Next
Hope this helps!
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, October 1, 2009 12:07 PM