c#如何编写整个数组如果字符串在"数组[1]"包含关键字

本文关键字:数组 quot 关键字 包含 何编写 字符串 如果 | 更新日期: 2023-09-27 18:17:36

我当前的代码正在循环遍历数组中包含保存字符串的列表。目前,它查找该数组中的所有字符串。我想改变这一点,使它只通过(搜索,寻找)字符串在"log[1]"

对不起,我不知道"log[1]"这个词。我是编程新手。继续读下去,我想你会明白的。

我想这样做:

foreach (string[] item[1] in loggbok)

item[1] = log[1]。第1条非常重要,因为我只想在log[1]内搜索。

这是我当前的代码保存整个数组在我的列表:

List<string[]> loggbok = new List<string[]> { };
string[] log = new string[3]; //date, title, post
DateTime date = DateTime.Now;
log[0] = "'n'tDate: " + date.ToLongDateString() + " Kl: " + date.ToShortTimeString();
Console.WriteLine(log[0]);
Console.Write("'tTitle: ");
log[1] = "'tTitle: " + Console.ReadLine();
Console.Write("'tPost: ");
log[2] = "'tPost: " + Console.ReadLine();
loggbok.Add(log);
log = new string[3];

我保存"log[1],log[2],log[3]"

下面的代码我想做一个搜索函数,通过我的列表,并识别log[1]内的所有字符串aka标题。如果字符串标题包含users关键字,则所有日志应连接并打印日志。

截至目前。我通过搜索所有日志(1,2,3)来解决这个问题。这意味着我的程序目前正在搜索(标题,日期,帖子)中的字符串。这使得当我希望用户只搜索标题时,您可以搜索消息或"帖子"。

所以我想如果在我的foreach循环中我把"item"变成"item[1]"。这会使我的代码只查找"log[1]"吗?我没有得到那么远,因为写"item[1]"是无效的语法。

当前搜索功能:

string key;
Console.Write("'n'tSearch: ");
key = Console.ReadLine();
//Searching through all log[] in loggbok.
//I want to change this line to item[1]
foreach (string[] item in loggbok)
{
    //goes deeper and looks for all strings within log[].
    foreach (string s in item)
    {
        //if a string is found containing key word, this block will run.
        if (s.Contains(key))
        {
            foundItem = true;
            Console.WriteLine(String.Join("'r'n", item));
            index++;
        }  
    }
}

c#如何编写整个数组如果字符串在"数组[1]"包含关键字

也许你可以这样做:

var result = loggbok.FirstOrDefault(x=> x.Any(s=> s.Contains(key));
Console.WriteLine(result?? "No record found");

您甚至不需要循环,因此您需要做的是根据索引从loggbok中检索条目。

// assign loggbokx of index 1, to variable item.
string[] item = loggbok[1];
// item will then have the 2nd (index=1) logbook.
// Note that index starts from 0.
// If you want to have the first one, then it should be loggbox[0]
// to make it even simpler you can write 
//     var item = loggbok[1];
// and the rest is the same...
//goes deeper and looks for all strings within log[].
foreach (string s in item)
{
    //if a string is found containing key word, this block will run.
    if (s.Contains(key))
    {
        foundItem = true;
        Console.WriteLine(String.Join("'r'n", item));
        index++;
    }  
}

让我们把它做好!

为日志创建一个模型类:

class LogEntry
{
    public DateTime Date { get; set; }
    public string Title { get; set; }
    public string Post { get; set; }
    public override string ToString()
    {
        return "Date: " + Date.ToLongDateString() + " Kl: " + Date.ToShortTimeString()
            + "'tTitle: " + Title + "'tPost: " + Post;
    }
}

现在我们可以放心地使用这个模型了。

让我们用更多的记录填充列表:

List<LogEntry> loggbok = new List<LogEntry>();
for (int i = 0; i < 5; i++)
{
    LogEntry entry = new LogEntry();
    entry.Date = DateTime.Now;
    entry.Title = "title" + i;
    entry.Post = "post" + i;
    loggbok.Add(entry);
}

打印出来:

foreach (var entry in loggbok)
    Console.WriteLine(entry);

由于ToString方法过载输出看起来很好。

让我们找点东西:

string key = "title3";
var found = loggbok.Find(log => log.Title == key);
Console.WriteLine("Found:'n" + found);

我们可以使用List类的不同方法,以及LINQ的扩展方法。


如果你需要保存你的数据到一个文件,然后从那里读取它们,你可以使用json序列化。

例如,让我们使用JavaScriptSerializer(不要忘记向程序集添加引用):

JavaScriptSerializer jss = new JavaScriptSerializer();
// Save
File.WriteAllText("test.txt", jss.Serialize(loggbok));
// Load
loggbok = jss.Deserialize<List<LogEntry>>(File.ReadAllText("test.txt"));

如果有人觉得有趣的话,这就是解决方案。

foreach (string[] item in loggbok)
{
   foreach (string s in item)
   {
      //This was the magic line.
      string searchTitle = item[1].ToLower();
      if (searchTitle.Contains(titleKey.ToLower()))
      {
         Console.WriteLine("'n'tSearch hit #" + index);
         foundItem = true;
         Console.WriteLine(String.Join("'r'n", item));
         index++;
         break;
      }
   }
}