c# - 如何从字符串中按顺序获取找到的字符并保留重复项以将其添加到列表中而不会覆盖以前的字符

原文 标签 c# string algorithm list

我想从字符串中每个单词的给定列表中查找字符,但是我不知道在我的情况下如何找到按单词存在的字符,并保持重复的字符。
例如,如果要查找的列表是: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();
        }
    }
}

然后对于最终输出,我希望从每个单词中得到所有这些找到的字符序列,将它们组合成一个字符串,在没有找到字符的地方用空格和下划线分隔。
所以不确定,但我想我必须将它添加到临时列表中,该列表必须在循环结束时收集结果才能附加到变量
我尝试过这种方法,但似乎它覆盖了以前的新添加:
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]

如果你有任何问题或建议,希望能让我知道。

相关文章:

java - 序列中2个项目之间的最小值

c# - 如何从DateTime.ToString()中排除秒

php - 关于字符串转换为int的性能

c++ - 如何优化非线性方程的解?

php - 如何使用PHP将字符串转换为数组

c - 将char指针数组转换为小写C

c++ - 使用Boost库查找图形的最小割

c# - 并非所选主题中的所有CSS文件都包含在页面中

c# - C#字符串中的空格后丢弃字符

c# - 在.NET C#中存储加密密钥的最佳方法