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]