简体   繁体   English

C#将文本数组拆分为子数组

[英]C# Split text array into sub array

I can't seem to find much on this online :( 我似乎在网上找不到很多东西:(

For my course I have to get text from a grocery.txt file and output an invoice. 对于我的课程,我必须从杂货店.txt文件中获取文本并输出发票。

The text file looks like this: 文本文件如下所示:

regular,bread,2.00,2
regular,milk,2.00,3
regular,cereal,4.00,1
fresh,rump steak,11.99,0.8
fresh,apple,4.60,1.00
fresh,cucumber,2.20,0.936
regular,cream,2.20,1
regular,mustard,3.30,1
fresh,carrots,1.79,1.5
regular,tomato sauce,2.95,1
regular,salt,2.80,1
fresh,bananas,4.00,1.2

Currently I use this method to get the text: 目前,我使用这种方法来获取文本:

    string[] groceryItemsArray = System.IO.File.ReadAllLines(@"C:\C\groceries.txt");

What I get is an array that in stores the entire line (for example "fresh,bananas,4.00,1.2"). 我得到的是一个存储整个行的数组(例如“ fresh,bananas,4.00,1.2”)。 I have no idea how to split the array into sub arrays or even whats the best way to go about this. 我不知道如何将数组拆分为子数组,或者什至是最好的方法。 Unfortunately this course task are way too advanced for the little material we have been taught and time we have for this. 不幸的是,对于我们所教的很少的材料和时间,该课程任务太过先进了。 I have already spend around 6 hours watching videos and getting not very far in learning the advanced stuff. 我已经花了大约6个小时来观看视频,并且对高级知识的学习还不太深入。

This is what I am trying to accomplish. 这就是我想要完成的。

A. Class named grocerrItem. A.名为grocerrItem的类。 This class has properties: name and price this class must have 2 constructors 此类具有属性:此类的名称和价格必须具有2个构造函数

A. subclass of this is a purchasedItem. A.的子类是PurchaseItem。 this class has an integer property 'quantity' one of its methods findCost, calculates the cost of the item, including 10% GST. 此类具有整数属性“ quantity”,其方法之一为findCost,用于计算商品成本,包括10%的GST。 the formula for this is price * quantity * 1.1 公式是价格*数量* 1.1

C. A subclass of this class (purchasedItem), freshItem has a property weight, a double type. C.此类的一个子类(purchasedItem),freshItem具有属性权重,为双精度类型。 The findCost method here, uses the formula:wieght * price, as it is not subject to GST. 此处的findCost方法使用公式:wieght *价格,因为它不受GST的影响。

Groceries.txt contains information as follows: Groceries.txt包含以下信息:

type(regular or fresh), name, price, quantity/weight - depending on being regular or fresh. 类型(常规或新鲜),名称,价格,数量/重量-取决于常规或新鲜。

** An invoice should be represented as a collection of grocery items (parent class). **发票应表示为杂货的集合(父类)。 This can be done using any container structure ie array, list or collection. 可以使用任何容器结构(即数组,列表或集合)来完成此操作。 you will then use iteration 然后,您将使用迭代

You can just use String.Split which returns you the sub array that you want. 您可以只使用String.Split ,它返回您想要的子数组。

public static void Main()
{
    string[] groceryItemsArray = System.IO.File.ReadAllLines(@"C:\C\groceries.txt");

    // now split each line content with comma. It'll return you an array (sub-array)
    foreach (var line in groceryItemsArray)
    {
        string[] itemsInLine = line.Split(',');

       // Do whatevery you want to do with this.
       // e.g. for 1st line itemsInLine array will contains 4 items 
       // "regular","bread","2.00", "2"
    }
}

You can use this code (I'm not sure what exactly you looking for): 您可以使用以下代码(我不确定您到底在寻找什么):

/// It's return all text as a single list
var groceryItemsArray = System.IO.File.ReadAllLines(@"C:\C\groceries.txt")
      .SelectMany(x => x.Split(new char[]{','} , StringSplitOptions.RemoveEmptyEntries)).ToList() ;

Or if want to return as Child List can use this code: 或者,如果要作为子列表返回,可以使用以下代码:

 var groceryItemsArray = System.IO.File.ReadAllLines(@"C:\C\groceries.txt").Select(x => 
           new { Child = x.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries) }).ToList();

I didn't really understand what are you trying to output, but here's some basic things I did understand on your question: 我不太了解您要输出的内容,但这是我对您的问题了解的一些基本知识:

( code not tested ) 代码未经测试

I created a parent class: 我创建了一个父类:

class GroceryItem 
{
    public string ItemName {get;set;}
    public decimal Price {get;set;}

    public GroceryItem() { }
}

And creates a child class: 并创建一个子类:

class PurchasedItem : GroceryItem
{
    public int Quantity { get;set; }
    public decimal Cost { get;set; }

    public PurchasedItem(string[] item)
    {
        ItemName = item[1];
        Price = decimal.Parse(item[2]);
        Quantity = int.Parse(item[3]);
        FindCost();
    }

    private void FindCost()
    {
        Cost = Price * Quantity * 1.1;
    }
}

Using your input to get all groceryItems, you can iterate using foreach loop and collect a list of purchasedItems. 使用您的输入来获取所有杂货项目,您可以使用foreach循环进行迭代并收集PurchasedItems的列表。

Main()
{
    string[] groceryItems = System.IO.File.ReadAllLines(@"C:\C\groceries.txt");

    var purchasedItems = new List<PurchasedItem>();
    foreach (var item in groceryItems)
    {
        string[] line = item.Split(',');
        purchasedItems.Add(new PurchasedItem(line));
    }
}

Well if you didn't understand this basic concept of programming, you better start on this link . 好吧,如果您不了解编程的基本概念,那么最好从此链接开始。

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

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