c# – 如何从字符串中按顺序获取找到的字符并保留重复项以将其添加到列表中而不覆盖以前的字符
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何从字符串中按顺序获取找到的字符并保留重复项以将其添加到列表中而不覆盖以前的字符,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3782字,纯文字阅读大概需要6分钟。
内容图文
![c# – 如何从字符串中按顺序获取找到的字符并保留重复项以将其添加到列表中而不覆盖以前的字符](/upload/InfoBanner/zyjiaocheng/825/5e67bd13e2474d0bb75ae9cddfc514f5.jpg)
我想分别从给定列表中找到每个字符串的字符,但是我无法弄清楚在我的情况下如何按字母顺序找到字符并保留重复的字符.
例如,如果要查找的列表是:var findChar = new List< string> {“a”,“i”,“t”,“e”,“c”};
因此,如果单词“关联”与我的输出是[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.
所以不确定,但我想我必须将它添加到临时列表中,该列表必须在周期结束时收集结果才能附加到变量
我试过这种方式,但似乎它覆盖了以前的新增加:
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# – 如何从字符串中按顺序获取找到的字符并保留重复项以将其添加到列表中而不覆盖以前的字符全部内容,希望文章能够帮你解决c# – 如何从字符串中按顺序获取找到的字符并保留重复项以将其添加到列表中而不覆盖以前的字符所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。