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]

如果您有任何问题或建议,希望对您有所帮助。

关于c# - 如何按顺序从字符串中获取找到的字符并保留重复项以将其添加到列表中而不覆盖以前的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39441947/

相关文章:

c# - 在 C# 中的新行上用分号拆分字符串

c# - 正则表达式用要替换的部分中的 "derived"替换所有出现的内容

java - Android 中的 String 有长度限制吗?

java - 只需用空格和一次一个字符更改字符位置,即可逐字符交换 2 个子字符串

php - 具有互斥性的二维数组的所有组合

java - 如何找到从0到180和-180到0的最近角度

c# - Set-cookie 不适用于 Dot net Core 3.1 中的跨站点请求/响应 & React 设置同站点 cookie 和/或 CORS 问题

c# - 如何使用 Application.Resources 中定义的 DataTemplate?

java - 在 ProgressBar 上绘制一个字符串,例如 JProgressBar?

python - 希望加快这个最大成对产品