1

我正在修改一个 .txt 文件,该文件在一列中包含多个名称的列表,它们的列出方式如下:

Lastname Middlename Name:
Lastname Middlename Name:
Lastname Middlename Name:
Lastname Middlename Name:
Lastname Middlename Name:

我只想保留每个名字的首字母并将它们添加到另一个列表中,例如:

姓 中间名 姓名:=> LAMN

我必须用空格分隔吗?或使用几个删除?或使用正则表达式?谢谢!

4

4 回答 4

3
var result = File.ReadAllLines("text.txt")
             .Select(line => new string (line.Split(' ')
                            .Select(s => s.First())
                            .ToArray())
               ).ToList();

编辑:

从姓氏中获取两个字母:

var result = File.ReadAllLines("text.txt").Select(line =>
            {
                var words = line.Split(' ');
                var la = words.First().Take(2);
                var mn = words.Skip(1).Select(s => s.First());

                return new string(la.Concat(mn).ToArray()).ToUpper();
            }
           ).ToList();

结果为:LAMN

于 2012-09-20T15:15:16.163 回答
2
public List<Person> ParseFile(string filePath)
{
    List<Person> lp = new List<Person>();
    using (StreamReader sr = new StreamReader(filePath))
    {
        while (!sr.EndOfStream)
        {
            lp.Add(new Person(sr.ReadLine()));
        }
    }
    return lp;
}

class Person
{
    public Person(string fullName)
    {
        this.fullName = fullName;
    }

    private string fullName;
    public string FullName
    {
        get { return fullName; }
        set { fullName = value; }
    }
    private string initials;

    public string Initials
    {
        get { return String.Join("",new string[]{
            String.Join("",fullName.Split(new char[] { ' ' }).Take(1).Select(i => i.Substring(0, 2))),
            String.Join("",fullName.Split(new char[] { ' ' }).Skip(1).Select(i => i.Substring(0, 1)))
        });
        set { initials = value; }
    }
}
于 2012-09-20T15:25:19.690 回答
1

首先,我将创建一个 Person 类:

public class Person
{
    string _initials = "";
    public String FirstName { get; set; }
    public String LastName { get; private set; }
    public String MiddleName { get; private set; }
    public String Initials { get { return _initials; } }
    public String FullName { get { return FirstName + MiddleName + LastName; } }

    public Person(String name)
    {
        string[] names = name.Split(' ');
        if (names.Length != 3)
        {
            throw new ArgumentException("Incorrect format for a person.");
        }
        FirstName = names[2];
        MiddleName= names[1];
        LastName  = names[0];
        _initials = 
             String.Concat(LastName[0],LastName[1],MiddleName[0],FirstName[0]);
    }
}

然后用文件填充 Person 类:

List<Person> personsList = new List<Person>();
using (StreamReader reader = new StreamReader(filePath))
{
    while (!reader.EndOfStream)
    {
        Person p = new Person(reader.ReadLine());
        personsList.Add(p);
    }
}

然后您可以访问 Person 的名字、姓氏和中间名以及他们的首字母:

foreach(Person p in personsList)
{
    Console.WriteLine(p.Initials);
}
于 2012-09-20T15:35:02.760 回答
0

可能它不是您问题的完整解决方案,但我认为这是在不使用 LINQ 的情况下获取首字母缩写的更好方法。

正则表达式从名称中提取首字母

于 2016-02-09T16:10:37.937 回答