1

I have this method:

public List<string> AdvMultiKeySearch(string key)
{
    string[] listKeys = key.Split(',');

    string[] ORsplit;
    List<string> joinedDashKeys = new List<string>();
    List<string> joinedSearchKeys = new List<string>();

    for (int i = 0; i < listKeys.Length; i++)
    {
        ORsplit = listKeys[i].Split('|');
        joinedDashKeys.Add(string.Join(",", ORsplit));
    }

    for (int i = 0; i < joinedDashKeys.Count; i++)
    {
        string[] split = joinedDashKeys[i].Split(',');
        for (int j = 0; j < split.Length; j++)
        {
            joinedSearchKeys.Add(string.Join(",", split[i]));
        }
    }

    return joinedDashKeys;
}

I am trying to create a method that receives a string Keyword that is composed of the words,comas, and '|' character. For example, user enters

glu|sal,1368|1199

And method should produce/return List of strings: "glu,1368", "glu,1199", "sal,1368", "sal,1199"

It's been more than two hours and I still can't figure out how to correctly implement it. Can someone help please?

8
  • Sorry, the expected output is "glu,1368", "glu,1199", "sal,1368", "sal,1199" Commented Jun 2, 2015 at 22:16
  • My bad, I thought that was your current output and not the expected one. Commented Jun 2, 2015 at 22:16
  • 1
    will there be more than 4 "string" as user's input? Commented Jun 2, 2015 at 22:16
  • If you mean more than 4 words in user input like glu|sal,1368|1199,info|cal then yes! Commented Jun 2, 2015 at 22:19
  • I think I can get wanted output by creating split.Length amount of items in the list and append the following indexes of the split array accordingly. Commented Jun 2, 2015 at 22:21

1 Answer 1

2

Given the input above this will show any number of combinations as long as there is one comma.

char[] splitter1 = new char[] { '|' };
char[] splitterComma = new char[] { ',' };
public List<string> AdvMultiKeySearch(string key)
{
    List<string> strings = new List<string>();

    string[] commaSplit = key.Split(splitterComma);
    string[] leftSideSplit = commaSplit[0].Split(splitter1);
    string[] rightSideSplit = commaSplit[1].Split(splitter1);

    for (int l = 0; l < leftSideSplit.Length; l++)
    {
        for (int r = 0; r < rightSideSplit.Length; r++)
        {
            strings.Add(leftSideSplit[l] + "," + rightSideSplit[r]);
        }
    }

    return strings;
}
Sign up to request clarification or add additional context in comments.

5 Comments

@Enigmativity no it does not. That has 2 commas.
@deathismyfriend - The OP made a comment that this should handle more than one comma.
@Enigmativity I asked him and he then said no that he doesn't need it for more than one comma.
@deathismyfriend - Oh, I see. The OP is being a bit confusing.
@Enigmativity it is ok. It happens

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.