gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:13:01 26 4
gpt4 key购买 nike

我试图从给定列表中分别为字符串的每个单词查找字符,但我无法弄清楚在我的情况下如何按单词中存在的顺序找到字符并保留重复的字符。

例如,如果要查找的列表是: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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com