简体   繁体   English

如果找到匹配项,如何将字符串保存在数组中并显示下一个字符串数组?

[英]How to save the strings in array and display the next string array if match found?

I read the *.txt file from c# and displayed in the console. 我从c#中读取* .txt文件,并显示在控制台中。

My text file looks like a table. 我的文本文件看起来像一张桌子。

diwas      hey
ivonne     how
pokhara    d kd
lekhanath  when
dipisha    dalli hos
dfsa       sasf

Now I want to search for a string "pokhara" and if it is found then it should display the "d kd" and if not found display "Not found" 现在,我要搜索字符串“ pokhara”,如果找到它,则应显示“ d kd”,如果找不到,则显示“未找到”

What I tried? 我尝试了什么?

string[] lines = System.IO.ReadAllLines(@"C:\readme.txt");
foreach(string line in lines)
{
    string [] words = line.Split();
    foreach(string word in words)
    {
        if (word=="pokhara")
        {
            Console.WriteLine("Match Found");
        }
    }
}

My Problem: Match was found but how to display the next word of the line. 我的问题:找到匹配项,但如何显示该行的下一个单词。 Also sometimes in second row some words are split in two with a space, I need to show both words. 另外有时在第二行中,有些单词会以空格分隔成两部分,我需要同时显示两个单词。

I guess your delimiter is the tab-character, then you can use String.Split and LINQ: 我猜你的分隔符是制表符,然后可以使用String.Split和LINQ:

var lineFields = System.IO.File.ReadLines(@"C:\readme.txt")
    .Select(l => l.Split('\t'));
var matches = lineFields
    .Where(arr => arr.First().Trim() == "pokhara")
    .Select(arr => arr.Last().Trim());
// if you just want the first match:
string result = matches.FirstOrDefault();  // is null if not found

If you don't know the delimiter as suggested by your comment you have a problem. 如果您不知道注释所建议的分隔符,则表示有问题。 If you don't even know the rules of how the fields are separated it's very likely that your code is incorrect. 如果您甚至不了解如何分隔字段的规则,则很可能您的代码不正确。 So first determine the business logic, ask the people who created the text file. 因此,首先确定业务逻辑,然后询问创建文本文件的人员。 Then use the correct delimiter in String.Split . 然后在String.Split使用正确的定界符。

If it's a space you can either use string.Split() (without argument), that includes spaces, tabs and new-line characters or use string.Split(' ') which only includes the space. 如果是空格,则可以使用包含空格,制表符和string.Split() (不带参数),也可以使用string.Split()包含空格的string.Split(' ') But note that is a bad delimiter if the fields can contain spaces as well. 但是请注意,如果字段也可以包含空格,则这是一个错误的分隔符。 Then either use a different or wrap the fields in quoting characters like "text with spaces" . 然后,要么使用其他字段,要么使用引号将字符括起来,例如"text with spaces" But then i suggest a real text-parser like the Microsoft.VisualBasic.FileIO.TextFieldParser which can also be used in C#. 但是,然后我建议使用像Microsoft.VisualBasic.FileIO.TextFieldParser这样的真实文本解析器,也可以在C#中使用它。 It has a HasFieldsEnclosedInQuotes property. 它具有HasFieldsEnclosedInQuotes属性。

This works ... 这有效...

  string[] lines = System.IO.ReadAllLines(@"C:\readme.txt");
    string stringTobeDisplayed = string.Empty;
    foreach(string line in lines)
    {
        stringTobeDisplayed = string.Empty;
        string [] words = line.Split();
         //I assume that the first word in every line is the key word to be found
          if (word[0].Trim()=="pokhara")
            {
                Console.WriteLine("Match Found");

                for(int i=1 ; i < words.Length ; i++)
                 {
                     stringTobeDisplayed += words[i]
                 }

                 Console.WriteLine(stringTobeDisplayed);

            }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM