Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate a string into a list where is not in a collection Dictionary

Tags:

c#

linq

Here is my sentence:

You ask your questions on StackOverFlow.com

Now I have a Dictionary collection of List of strings:

Dictionary<int, List<string>> collections = new Dictionary<int, List<string>>()
{
    { 1, new List<string>() { "You", "your" } },
    { 2, new List<string>() { "Stack", "Flow" } },
};

I need to split my string into a list of the string where the strings in the collection are not there and put each List key in its place so I know from which list I'm finding the list's item:

for example, the output of my sentence is:

"1", " ask ", "1", " questions on ", "2", "Over", "2", ".com"

as you can see all of the words in the collection are not listed here. Instead, their list's key is replaced. All I know is that I can check if my string contains any words of another list or not by this code:

listOfStrings.Any(s=>myString.Contains(s));

But do not know how to check an entire dictionary collection of lists and get each list key and replace it in the new list of strings.

like image 590
Inside Man Avatar asked Dec 10 '25 06:12

Inside Man


2 Answers

static void Main()
{
    string myString = "You ask your questions on StackOverFlow.com";
    string[] collection = new string[]
    {
        "You",
        "your",
        "Stack",
        "Flow"
    };

    var splitted = myString.Split(collection, StringSplitOptions.RemoveEmptyEntries);

    foreach(var s in splitted)
    {
        Console.WriteLine("\""+s+"\"");
    }
}

will result in:

" ask "
" questions on "
"Over"
".com"

You can use SelectMany to flatten your collections


Based on your edit:

static void Main()
{
    string myString = "You ask your questions on StackOverFlow.com";

    Dictionary<int, List<string>> collections = new Dictionary<int, List<string>>()
    {
        { 1, new List<string>() { "You", "your" } },
        { 2, new List<string>() { "Stack", "Flow" } },
    };

    foreach(var index in collections.Keys)
    {
        foreach(var str in collections[index])
        {
            myString = myString.Replace(str, index.ToString());
        }
    }

    Console.WriteLine(myString);
}

Gives:

1 ask 1 questions on 2Over2.com

static void Main()
{
    string myString = "You ask your questions on StackOverFlow.com";

    Dictionary<int, List<string>> collections = new Dictionary<int, List<string>>()
    {
        { 1, new List<string>() { "You", "your" } },
        { 2, new List<string>() { "Stack", "Flow" } },
    };

    var tempColl = collections.SelectMany(c => c.Value);
    // { "You", "your", "Stack", "Flow" }

    string separator = String.Join("|", tempColl);
    // "You|your|Stack|Flow"

    var splitted = Regex.Split(myString, @"("+separator+")");
    // { "", "You", " ask ", "your", " questions on ", "Stack", "Over", "Flow", ".com" }

    for(int i=0;i<splitted.Length;i++)
    {
        foreach(var index in collections.Keys)
        {
            foreach(var str in collections[index])
            {
                splitted[i] = splitted[i].Replace(str, index.ToString());
            }
        }
    }

    foreach(var s in splitted)
    {
        Console.WriteLine("\""+s+"\"");
    }
}

Gives:

""
"1"
" ask "
"1"
" questions on "
"2"
"Over"
"2"
".com"

Note the empty string at the beginning is because

If a match is found at the beginning or the end of the input string, an empty string is included at the beginning or the end of the returned array.

But you can easily remove it using either .Where(s => !string.IsNullOrEmpty(s)) or anything alike.

like image 191
Rafalon Avatar answered Dec 11 '25 19:12

Rafalon


You can flatten the list using the SelectMany linq function. This new list of strings can be fed to the Split function.

var str = "You ask your questions on StackOverFlow.com";
var collections = new[]
{
    new List<string> { "You", "your" },
    new List<string> { "Stack", "Flow" },
};

var values = str.Split(
    collections.SelectMany(s => s).Distinct().ToArray(), 
    StringSplitOptions.RemoveEmptyEntries
);

Console.WriteLine(
    string.Join(", ", values)
);

The code above results in

ask , questions on , Over, .com

like image 37
Jerodev Avatar answered Dec 11 '25 20:12

Jerodev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!