[Solved] C# Full-text search string format : string remove all adjacent duplicates and append with ‘AND’ ‘OR’ [closed]


Since you haven’t shown what you’ve done so far I’m assuming that you haven’t started on a solution, so here’s a high level algorithm:

In that case, use String.Split(' ') to split the searchstring by each space.

Use a foreach loop on the resulting array of strings and use string concatenation to complete, if a word was already used before that’s not or or and, don’t add it to the resulting string.
If the previous word was or or and and the current one also is, don’t add it to the resulting string.
If the previous word wasn’t or or and and the current one isn’t, add or to the resulting string.

EDIT: Now that the code has been posted, I can see what’s wrong

this conditional:

    if (output.Contains(" ") && !output.Contains("and") && !output.Contains("or"))
    {
        return string.Join(" or ", output.Split(' ').Select(I => I.Trim()));
    }

is only getting called if the output doesn’t contain any instance of and or or

Do the check to see if or needs to be added within your foreach loop, and get rid of that conditional

e.g:

            foreach (var item in strArray)
            {
                currStr = item.Trim();
                keywordFlag = searchKeywords.Contains(prevStr) && searchKeywords.Contains(currStr);
                duplicateFlag = outputArray.Contains(currStr) && !searchKeywords.Contains(currStr);
                if (!currStr.Equals(prevStr) && !keywordFlag && !duplicateFlag)
                {
                    if (!searchKeywords.Contains(prevStr) && !searchKeywords.Contains(currStr) && prevStr != "")
                    {
                        outputArray.Add("or");
                    }
                    outputArray.Add(currStr);
                    prevStr = currStr;
                }
            }

Also, where you’re checking to see if there are only 2 tokens in the array, you’re only accounting for if they put or or and after a word, what happens if they put in or Oyster as an input string? The resulting string would just be or

you need to account for that:

            if (outputArray.Count() == 2)
            {
                if(searchKeywords.Contains(outputArray[0]))
                    outputArray.Remove(outputArray[0]);
                else if(searchKeywords.Contains(outputArray[1]))
                    outputArray.Remove(outputArray[1]);
            }

3

solved C# Full-text search string format : string remove all adjacent duplicates and append with ‘AND’ ‘OR’ [closed]