Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I have a string in C# like this: "Hello all friends"
and how can I have a string like this:
"(Hello AND all AND Friends) OR (Hello AND all) OR (Hello AND friends) OR (all AND friends) OR (Hello) OR (all) OR (friends) "
?

What I have tried:

because I though I am very Disappointment
Posted
Updated 30-Sep-20 1:07am
Comments
BillWoodruff 30-Sep-20 5:40am    
How can you be disappointed when you haven't tried anything ?
F-ES Sitecore 30-Sep-20 8:45am    
What you're trying to do is a bad idea. If this is a public search function your code can perform incredibly badly if someone puts in a long phrase, so bad it can tie up a thread until it times out and you have a possible DOS attack vector. If this is an index search just throw all the words as optional requirements and let the index do the work for you.

Look at the String Class (System) | Microsoft Docs[^] and StringBuilder Class (System.Text) | Microsoft Docs[^]. Between them they contain properties and methods that can solve most such issues.
 
Share this answer
 
Split the string into words. If the words are always separated by single spaces, you can use String.Split[^]; otherwise, you might need to use a regular expression:
C#
string[] words = System.Text.RegularExpressions.Regex.Matches(input, @"\w+")
    .Cast<System.Text.RegularExpressions.Match>()
    .Select(m => m.Value)
    .ToArray();
Regular Expression Language - Quick Reference | Microsoft Docs[^]

Then you need to generate all combinations of words. Eric Lippert has a nice series of blog posts explaining how to do that:
Producing combinations, part one | Fabulous adventures in coding[^]

Or you can look at the implementations on Rosetta Code:
Combinations - Rosetta Code[^]

A simple version might look something like this:
C#
static IEnumerable<IReadOnlyList<T>> AllCombinations<T>(IReadOnlyList<T> input)
{
    for (int m = input.Count; m >= 1; m--)
    {
        var result = new T[m];
        var stack = new Stack<int>();
        stack.Push(0);
        
        while (stack.Count > 0)
        {
            int index = stack.Count - 1;
            int value = stack.Pop();
            while (value < input.Count)
            {
                result[index] = input[value];
                index++;
                value++;
                
                stack.Push(value);
                
                if (index == m)
                {
                    yield return result;
                    break;
                }
            }
        }
    }
}

You then combine the two:
C#
IEnumerable<string> parts = AllCombinations(words).Select(c => string.Join(" AND ", c));
string result = "(" + string.Join(") OR (", parts) + ")";
 
Share this answer
 
You have to
  • Split the string into its words (an array of strings)
  • make a loop, with n=1,..,len where len is the length of the words array
  • at iteration n, find the combinations of n items of the words array
  • iterate over the found combinations printing them properly formatted


Findindig the combinations could be tricky. This page gives great help: c# - How to use LINQ to find all combinations of n items from a set of numbers? - Stack Overflow[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900