写出列表的特定行 [英] Writing out a specific line of a list

查看:119
本文介绍了写出列表的特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大文件,我把这个文件,并分为两组:A和B.下面是该文件中的每一行的示例:

I have a large file, and I took that file and divided it into 2 groups: A and B. Below is a sample of each line in the file:

 5/21/2015 11:55:56 PM | Batch 6|386/767|50.33 %|CH2M-R|Processed NXRMN5...Checking refundable and non-refundable fares. Traditional Booking. Inside ticketing window. Minimum Savings Required: $131.00. Actual Savings: $257.18. Savings found: $257.18 (11.55 %). Savings were previously found.

简而言之,我编写一个程序来查看A组和B组,如果一行有Savings found:,它会写出那行。此外,如果该文件节省30%或更大,它将写出该行,并在前面放1个星号,如果它节省30%或更大和500美元或更大,它将写出2个星号。为了澄清,257.18是找到的节余量,该数字旁边的百分比是必须找到的百分比。

In a nutshell, I am writing a program that will look into both groups A and B, and if a line has "Savings found:" it will write out that line. Also if that file has a saving of 30% or greater, it will write out that line and put 1 asterisk in front, and if it has savings of 30% or greater and $500 or greater, it will write out 2 asterisks. For clarification, the 257.18 is the amount of savings that was found, and the percent next to that number is the percentage that has to be found.

这是我到目前为止的if语句,我只需要在if语句里面的代码:

This is the if statement that I have so far, I just need the code that has to go inside the if statement:

if (a.Any(item => item.Contains("Savings found:"))
{

}

我是c#的新手,我不知道如何让控制台写特定的代码行

I am new to c# and I have no idea how to make the console write the specific line of code that has these requirements. Any help will be appreciated!

推荐答案

我认为我们正在帮助一个学校计划在这里。

I think we're helping out with a school program here. There were several questions yesterday that dealt with almost the exact same issue and data.

同时,由于你想迭代你的结果集来寻找确切的节省,我将会推荐以下,而不是使用if。使用Where查询将与任何相同,但将返回一个集合,而不是布尔值。直接进入foreach将有助于保存

In the mean time, since you want to iterate your result set to look for the exact savings, I would recommend the following, instead of using the "if". Using the "Where" query will do the same as the "Any", but will return a collection, instead of a boolean. Going straight into the "foreach" will help save a step too.

var find = "Savings found:";

foreach(var line in a.Where(w => w.Contains(find))
{
  var subStr = line.Substring(0, line.IndexOf(find)+find.Length);
  var startIndex = subStr.IndexOf('(');
  var endIndex = subStr.IndexOf(')');

  var savings = double.Parse(subStr.SubString(0, startIndex-1).Trim());
  var percent = double.Parse(subStr.SubString(startIndex+1, endIndex-startIndex-2).Trim());

  Console.WriteLine("{0}{1}{2}", (percent >= 30) ? "*" : string.Empty,
                                 (percent >= 30 && savings >= 500) ? "*" : string.Empty,
                                 line);
} 

从这里你可以解析行数据找到节省,输出您需要的任何数据。

From here you can parse the line data to find the savings and you can write out whatever data you need to.

解析字符串基本上只是将字符串数据分成不同的部分,尝试获取所需的数据。但由于您只是寻找百分比,请检查上面的代码进一步的例子

Parsing the string is basically just breaking the string data into different parts to try and get the data you want. But since you're just looking for the percentage, check the code above for a further example

这篇关于写出列表的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆