如何在.txt文件中找到分配给名称的值? [英] How do I find a value that is assigned to a name in .txt file?

查看:84
本文介绍了如何在.txt文件中找到分配给名称的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个.txt文件,里面有车名,每辆车的数量,制造年份和价格:

For example, i got a .txt file in which there are cars names, amount of each car, year they were made and their price:

5
ORION; 2; 1935; 365.1;
POLARIS; 1; 1988; 87.09;
INDUSTRIERAD; 10; 1995; 58.10;
Bauer; 25; 2008; 285.58;
HERREN; 15; 2012; 1040.42;



我找到周期为1040.42的最大值。所以接下来我需要找到最大值名称(Herren),那辆车的数量和它的年份。

所以问题是我不知道怎么找所有这些。我需要使用for和if,但是havo不知道该怎么做。



我尝试了什么:



互联网上的所有内容。


I find the max value with for cycle that is 1040.42. So next i need to find the max values name(Herren), the amount of of that car that there is and the year it was made.
So the problem is i don't know how to find all of this. I need to use "for" and "if", but havo no idea how to do it.

What I have tried:

Everything that is on the internet.

推荐答案

看看这个 CodeProject 文章: C# - 轻量级和快速CSV解析器 [ ^ ]。
Have a look at this CodeProject article: C# - Light and Fast CSV Parser[^].


首先创建一个根据您的数据表示的POCO:

First create a POCO that represents according to your data:
public class Car
{
	public string Name { get;set;}
	public int Amount { get;set;}
	public int Year { get;set;}
	public decimal Price { get;set;}
	
	public override String ToString()
	{
		return String.Format("Name:{0},Amount{1},Year:{2},Price:{3}",Name,Amount,Year,Price);
	}
}





然后读取文件然后创建一个集合然后使用linq获取价格最高的汽车并打印出来:





and then read the file and then create a collection and then use linq to get the car with highest price and print it out:

var lines = File.ReadAllLines("cars.text");

int totalCars = Convert.ToInt32(lines[0]);
		List<Car> cars = new List<Car>();
		for(int i = 1; i < totalCars; i++)
		{
			var line = lines[i];
			var carInfo = line.Split(';');
			
			Car car = new Car();
			car.Name = carInfo[0];
			car.Amount = Convert.ToInt32(carInfo[1]);
			car.Year = Convert.ToInt32(carInfo[2]);
			car.Price = Convert.ToDecimal(carInfo[3]);
			
			cars.Add(car);
		}
		
		var carWithHighestPrice = cars.OrderByDescending(x=>x.Price).First();
		
		Console.WriteLine(carWithHighestPrice);


这篇关于如何在.txt文件中找到分配给名称的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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