C# 筛选文件中的文本

本文关键字:文本 文件 筛选 | 更新日期: 2023-09-27 18:32:15

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

John Doe         123456789 CD MM
Jane Doe         789456132 AB FM

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

C# 筛选文件中的文本

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

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

所以根据你的评论,

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
}

将"人员"类更改为

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);
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")); 
有很多

方法可以做到这一点。一个已经使用 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;
    }
}