Daniel Sky
Daniel Sky

Reputation: 39

Create List of strings from the string inputted by the user c#

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?

Upvotes: 1

Views: 75

Answers (1)

deathismyfriend
deathismyfriend

Reputation: 2192

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;
}

Upvotes: 2

Related Questions