我想分别从给定列表中找到每个字符串的字符,但是我无法弄清楚在我的情况下如何按字母顺序找到字符并保留重复的字符.
例如,如果要查找的列表是: var findChar = new List<string> { "a", "i", "t", "e", "c" }; 
因此,如果"associates"我的输出的单词是[a i t e c]我想要的结果应该[a c i a t e]按顺序(A)sso(c)(i)(a)(t)(e)s 
这是我的代码,你能帮我找出如何得到这个输出的预期结果:
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();
        }
    }
}
然后对于最终输出我希望从每个单词中获取所有这些已找到的字符序列,将它们全部组合在一个用空格和下划线分隔的字符串中,其中没有找到char.
所以不知道,但我想我必须将它添加到必须收集在循环的最终结果是武官variable的临时列表
我试过这种方式,但似乎它覆盖了以前的新增加:
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));
    }
当前输出:
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]
期望的输出:
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] 
这是一种方法:
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
    foreach (var character in s.Where(c => findChar.Contains(c.ToString())))
    {
        foundChar.Add(character);
    }
    // Part of the output string containing the found characters
    var charString = (foundChar.Count > 0) ? string.Join(" ", foundChar) : " _ ";
    Console.WriteLine($"({wordNumCount}) [{word}] [{charString}]");
    finalOutputStrings.Add(charString.Replace(" ", ""));
}
var outputString = string.Join(" ", finalOutputStrings);
Console.WriteLine($"Result output: [{outputString}] ");
存储最终输出字符串,在finalOutputStrings评估每个字后添加.
请注意,对于每个已评估的单词,我将围绕单词中的每个字符进行迭代,以保留这些单词出现的顺序.
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]
希望如果您有任何问题或建议,请帮助我.