LadO LadO - 2 months ago 6
C# Question

How to get found characters by order from string and keep duplicates to add it to the list without overwriting of previous

I'm tying to find characters from given list for each word of string separately, but I can't figure out how in my case to get found character by order exist in word and keep repeated characters.

For example, if list to find is:

var findChar = new List<string> { "a", "i", "t", "e", "c" };


So if word
"associates"
with my output is
[a i t e c]
my desired result should be
[a c i a t e]
by sequence
(A)sso(c)(i)(a)(t)(e)s


Here is my code, can you help me find out how can I get desired result for this outputs:

using System;
using System.Collections.Generic;
using System.Linq;

namespace _01_WORKFILE
{
class Program
{
static void Main(string[] args)
{
var findChar = new List<string> { "a", "i", "t", "e", "c" };

List<string> displist = findChar;
Console.WriteLine("Search for chars: [{0}]", string.Join(", ", displist));

int wordNumCount = 0;
char[] delimiterChars = { ' ', ',', '.', ':', '\t' };

string text = "Offices of Runciter Associates";
string[] words = text.Split(delimiterChars);

Console.WriteLine("In string (" + text + ") consisting of (" + words.Length + ") words: ");

foreach (string s in words)
{
if (findChar.Any(s.Contains))
{
wordNumCount++;
var foundChar = string.Join(" ", findChar.Where(ch => s.Contains(ch)));
Console.WriteLine("(" + wordNumCount + ") [" + s + "] [" + foundChar + "]");
}
else
{
wordNumCount++;
var foundChar = string.Join(" ", findChar.Where(ch => s.Contains(ch)));
Console.WriteLine("(" + wordNumCount + ") [" + s + "] [ _ ]");
}
}
Console.Read();
}
}
}


and then also for ultimate output I want get all this sequences of found character from each word to combine it all in one string separated with spaces and underscore where char is no found.

So not sure but I guess I have to add it to the temporary list which must collect results at the end of cycle to be attache to variable

I've tried this way, but seems like it overwrite new add over the previous:

if (findChar.Any(s.Contains))
{
wordNumCount++;
var foundChar = string.Join(" ", findChar.Where(ch => s.Contains(ch)));

List<string> tempList = new List<string>();
tempList.Add(foundChar);
Console.WriteLine("To result output: [{0}]", string.Join(", ", tempList));
}
else
{
wordNumCount++;
var foundChar = string.Join(" ", findChar.Where(ch => s.Contains(ch)));

List<string> tempList = new List<string>();
tempList.Add(foundChar);
Console.WriteLine("To result output: [{0}]", string.Join(", ", tempList));
}


Current output:

Search for chars: [a, i, t, e, c]
In string (Offices of Runciter Associates) consisting of (4) words:
(1) [Offices] [i e c]
(2) [of] [ _ ]
(3) [Runciter] [i t e c]
(4) [Associates] [a i t e c]


Desired output:

Search for chars: [a, i, t, e, c]
In string (Offices of Runciter Associates) consisting of (4) words:
(1) [Offices] [i c e]
(2) [of] [ _ ]
(3) [Runciter] [c i t e]
(4) [Associates] [a c i a t e]
Result output: [ice _ cite aciate]

Answer

Here is one way to do it:

var finalOutputStrings = new List<string>();
foreach (string word in words)
{
    wordNumCount++;
    // Lowercase the input word to recognise capitals
    var s = word.ToLower();
    // List to store the found characters
    var foundChar = new List<char>();

    // Iterate around each char in the word to retain the desired order
    for (int i = 0; i < s.Count(); i++)
    {
        if (findChar.Contains(s[i].ToString()))
        {
            foundChar.Add(s[i]);
        }
    }

    // Part of the output string containing the found characters
    var charString = string.Empty;

    // Building the output string for this word
    if (foundChar.Count > 0)
    {
        charString = string.Join(" ", foundChar);
    }
    else
    {
        charString = " _ ";
    }

    Console.WriteLine($"({wordNumCount}) [{word}] [{charString}]");
    finalOutputStrings.Add(charString.Replace(" ", ""));
}
var outputString = string.Join(" ", finalOutputStrings);
Console.WriteLine($"Result output: [{outputString}] ");

The final output strings are stored in finalOutputStrings, which is added to after each word is evaluated.

Notice that for each evaluated word, I am iterating around each character within the word to retain the order in which those characters appear.

Output

Search for chars:  [a, i, t, e, c]
In string (Offices of Runciter Associates) consisting of (4) words: 
(1) [Offices] [i c e]
(2) [of] [ _ ]
(3) [Runciter] [c i t e]
(4) [Associates] [a c i a t e]
Result output: [ice _ cite aciate]

Hope that helps let me know if you have any questions or suggestions.