C#从文件中过滤文本

时间:2014-11-28 02:02:26

标签: c# arrays filter

我正在尝试根据最后4个字符过滤这样的文本数组。

John Doe         123456789 CD MM
Jane Doe         789456132 AB FM

因此,如果我将过滤器设置为MM I,那么我就不应该看到Jane Doe。

4 个答案:

答案 0 :(得分:2)

String.EndsWith让您检查最后一个字符是否是您想要的字符。

var source = new [] { "John Doe  123456789 CD MM", "Jane Doe 789456132 AB FM" };
var filtered = source.Where(s => s.EndsWith("MM"));

答案 1 :(得分:0)

所以根据你的评论,

John Doe         123456789 CD MM
Jane Doe         789456132 AB FM

它们现在已经分开了属性,所以我假设你有这样的类结构。

public class Person
{
    public string Name { get; set; }

    public int ID { get; set; }

    public string Tag { get; set; }

    public string Sex { get; set; }
}

你有一个List<Person>或Person []来存储你的收藏。您可以使用Linq查询按性别过滤它。这样做。

        Person[] coll = new Person[] 
        {
            new Person
            {
                ID = 123456789,
                Name = "John Doe",
                Tag = "CD",
                Sex = "MM"
            },
            new Person
            {
                ID = 123456789,
                Name = "Jane Doe",
                Tag = "AB",
                Sex = "FM"
            }
        };

        var filteredSex = coll.Where(x => x.Sex == "MM");

你甚至可以将TagSex作为枚举值,声明如下。

public enum Sex
{
    MM,
    FM
}

public enum Tag
{
    CD,
    AB
}

将您的Person类更改为

public class Person
{
    public string Name { get; set; }

    public int ID { get; set; }

    public Tag Tag { get; set; }

    public Sex Sex { get; set; }
}

并像这样分配。

        Person[] coll = new Person[] 
        {
            new Person
            {
                ID = 123456789,
                Name = "John Doe",
                Tag = Tag.CD,
                Sex = Sex.MM
            },
            new Person
            {
                ID = 123456789,
                Name = "Jane Doe",
                Tag = Tag.AB,
                Sex = Sex.FM
            }
        };

        var filteredSex = coll.Where(x => x.Sex == Sex.MM);

答案 2 :(得分:0)

String[] source = { "John Doe  123456789 CD MM", "Jane Doe 789456132 AB FM" };

foreach(var item in source)
{
    if(item.EndsWith("MM"))
    {
        // do stuff
    }
}

or you can use LINQ

// This will give you a list of items containing strings ending with MM
var result = source.Where(x=> x.EndsWith("MM")); 

答案 3 :(得分:0)

有很多方法可以做到这一点。一个已经使用LINQ发布了。这是一种直截了当的方式。这不是一个最佳的解决方案,C#提供了更多的方法来使它变得更好,但我的意思是让它变得简单。

namespace ConsoleApplication18
{
    class Program
    {
        static void Main(string[] args)
        {


            Students s1 = new Students();
            s1.name = "John Doe";
            s1.age = 12m;
            s1.tag = "MM";

            Students s2 = new Students();
            s2.name = "Su";
            s2.age = 12m;
            s2.tag = "XX";

            Students s3 = new Students();
            s3.name = "Jane Doe";
            s3.age = 12m;
            s3.tag = "FM";

            Students[] arr = new Students[3];
            arr[0] = s1;
            arr[1] = s2;
            arr[2] = s3;


            //Main logic
            string result = "";
            //loop in all array elements
            for (int i = 0; i < arr.GetUpperBound(0)+1 ; i++)
            {
                if (arr[i].tag == "MM") //Choose 0, 1 or more enteries with MM in tag
                {
                    result = result + arr[i].name + " " + 
                                      arr[i].age.ToString() + " " + 
                                      arr[i].tag +
                                      "  Position:" + i.ToString() + 
                                      Environment.NewLine;
                }

            }

            if (result != "")
            {
                Console.WriteLine(result);
            }

            Console.Read();

        }
    }

    public class Students
    {
        public string name;
        public decimal age;
        public string tag;
    }
}