Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中获取特定字符串后的文本行#_C# - Fatal编程技术网

C# 在C中获取特定字符串后的文本行#

C# 在C中获取特定字符串后的文本行#,c#,C#,我正在读取一个类似这样的文件的内容 [1] Message=my string here EffDate=1/1/1995 DeactDate= Modified= Note= [2] Message=my other string here EffDate=1/1/1995 DeactDate= Modified= Note= string[] getMessages(string file) { List<string> messages = new List

我正在读取一个类似这样的文件的内容

[1]
Message=my string here
EffDate=1/1/1995
DeactDate= 
Modified= 
Note=

[2]
Message=my other string here
EffDate=1/1/1995
DeactDate= 
Modified= 
Note=
string[] getMessages(string file) {
    List<string> messages = new List<string>();

    string[] lines = file.ReadAllLines();       /* read the lines */
    foreach(string line in lines) {
        string[] parts = line.Split('=');
        string contentPart = String.Join("=", parts.Skip(1).ToArray();
        if(parts[0]=="Message")
            messages.Add(contentPart);
    }

    return messages.ToArray();
}
我想获取
Message=
之后的字符串,但仅限于行的末尾。我不想要任何日期、笔记或任何东西

我希望将这两条消息都放入字符串数组或类似的东西中。有什么办法吗


非常感谢您的帮助。

您可以使用followin方法

  • 阅读您的文件,最好使用ReadAllLines方法,请看

  • 阅读后,您将获得字符串集合,每行一项

  • Write方法检查文件中的行是否从消息开始
  • 如果是,请使用带有“=”符号的String.Split方法,并将索引1的元素添加到消息的输出集合中

  • 我希望这是可以理解的。

    你可以这样做

    [1]
    Message=my string here
    EffDate=1/1/1995
    DeactDate= 
    Modified= 
    Note=
    
    [2]
    Message=my other string here
    EffDate=1/1/1995
    DeactDate= 
    Modified= 
    Note=
    
    string[] getMessages(string file) {
        List<string> messages = new List<string>();
    
        string[] lines = file.ReadAllLines();       /* read the lines */
        foreach(string line in lines) {
            string[] parts = line.Split('=');
            string contentPart = String.Join("=", parts.Skip(1).ToArray();
            if(parts[0]=="Message")
                messages.Add(contentPart);
        }
    
        return messages.ToArray();
    }
    
    string[]获取消息(字符串文件){
    列表消息=新列表();
    string[]lines=file.ReadAllLines();/*读取行*/
    foreach(行中的字符串行){
    string[]parts=line.Split('=');
    string contentPart=string.Join(“=”,parts.Skip(1.ToArray();
    如果(部分[0]=“消息”)
    messages.Add(contentPart);
    }
    返回消息。ToArray();
    }
    
    尽管请记住,这不是一个非常优雅和有效的解决方案,但最好是解析整个文件,填充一个结构数组,然后对其进行操作


    为了说明原因,我使用了
    Split
    而不是其他任何东西:当比较和尝试列出的不仅仅是
    消息时,如果您已经在读取文件,那么使用
    StartsWith
    Contains
    将变得无效,因为您只需要将第一行存储在一个字符串中ch包含字符串
    消息
    ,然后使用
    =
    拆分字符串并存储在列表中

    string strFilePath = @"D:\Maraj\Work\Ajax.txt";
    FileStream objFS = new FileStream(strFilePath, FileMode.Open);
    List<string> list = new List<string>();
    using (StreamReader Sr = new StreamReader(objFS))
      {
        while (Sr.ReadLine() != null)
        {
        string line = Sr.ReadLine();
        if(line.Contains("Message=")
          {
            list.Add(line .Split('=')[1]);
          }
        }
         objFS.Close();
      }
    
    string strFilePath=@“D:\Maraj\Work\Ajax.txt”;
    FileStream objFS=newfilestream(strFilePath,FileMode.Open);
    列表=新列表();
    使用(StreamReader Sr=新StreamReader(objFS))
    {
    while(Sr.ReadLine()!=null)
    {
    字符串行=Sr.ReadLine();
    if(line.Contains)(“Message=”)
    {
    list.Add(line.Split('=')[1]);
    }
    }
    objFS.Close();
    }
    
  • 读课文中的所有行
  • 对于每行,检查行的开头是否存在消息=
  • 如果是,则使用Substring()方法获取消息

    var messages=File.ReadAllLines(“M:\\M.txt”);
    列表结果=新列表();
    foreach(消息中的var msg)
    {
    var key=“Message=”;
    如果(消息启动(键))
    添加(msg.Substring(msg.IndexOf(key)+key.Length));
    }
    

  • 这是您需要的工作代码:

    var d = File.ReadAllLines(filePath);
                var t = d.Where(g => g.Contains("Message"));
                string[] splited;
                foreach (var item in t)
                {
                    splited = item.Split(new string[] { "Message=" }, StringSplitOptions.None);
                    Console.WriteLine(splited[1]);
                }
    

    如果您有任何疑问,请告诉我。

    您只需要一个简单的
    循环:

    string[] messages = File.ReadAllLines("data.txt"); //read all lines
    
    for (int i = 1; i < messages.Length; i = i + 7) //just get the line containing "Message"
    {
        string message = messages[i].Split('=')[1];
        //now you have the message, save it where ever you want.
    }
    
    string[]messages=File.ReadAllLines(“data.txt”);//读取所有行
    for(inti=1;i
    这就足够了:

    string[] messages = File.ReadLines("fileName.txt")
                            .Where(line => line.StartsWith("Message="))
                            .Select(line => line.Split(new char[] { '=' }, 2)[1])
                            .ToArray();
    

    您不能使用split on=,因为它可以是Message=之后字符串的一部分。有疑问的是,User is说过该行将包含“Message=”;\但是他没有说=在那一行只出现一次。为了清楚起见,请看一下我的答案。你不能使用split on=,因为它可以是Message=之后字符串的一部分。@MaheshChand没有说明。无论如何,我做了一个更正编辑。仍然,错了。而且,他没有提到=在那一行只出现一次。恕我直言ar,请看一看我的答案。@MaheshChand为什么错了?你知道可能有多个正确答案,而不仅仅是你的答案?如果文本像..Message=我的字符串,你的答案会失败,并且它也包含=并且它希望它成为结果,你不能使用split on=,因为它可以是Message=之后的字符串的一部分。你不能使用split on=s因为它可以是Message=.之后字符串的一部分。@MaheshChand:有没有用户提到过,=符号可以出现一次或多次?为什么你投了反对票?谢谢,非常有用的答案。我可以问一下为什么我需要调用拆分的
    的第一个索引吗?是第0个索引
    Message=
    ?是的。Message=将位于第0个索引,其余的是line将位于第一个索引。@WesThompson您的
    消息
    值是否包含任何
    =
    ?如果文本像..Message=my字符串,并且它也包含=并且它希望它位于result@MaheshChandOP提供的测试用例不包含任何
    =
    ,只包含
    消息LD考虑OP已经声明了什么,不应该和我们的假设一致。@ MaheshChand不应该因为你的假设而投票所有正确的答案。所有的答案都是正确的,不同的方法。