C# 从数组中选择不同的名称
我想知道如何从数组中仅选择不同的名称。 我所做的是从包含许多不相关信息的文本文件中读取。 我当前代码的输出结果是一个名称列表。我只想从文本文件中选择每个名称中的 1 个。
以下是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
namespace Testing
{
class Program
{
public static void Main(string[] args)
{
String[] lines = File.ReadLines("C:\\Users\\Aaron\\Desktop\\hello.txt").ToArray();
foreach (String r in lines)
{
if (r.StartsWith("User Name"))
{
String[] token = r.Split(' ');
Console.WriteLine(token[11]);
}
}
}
}
}
I would like to know how to select only the distinct names from an array.
What I did was to read from a text file which contains many irrelevant information.
My output results for my current codes is a list of names. I want to select only 1 of each name from the text file.
Following are my codes:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
namespace Testing
{
class Program
{
public static void Main(string[] args)
{
String[] lines = File.ReadLines("C:\\Users\\Aaron\\Desktop\\hello.txt").ToArray();
foreach (String r in lines)
{
if (r.StartsWith("User Name"))
{
String[] token = r.Split(' ');
Console.WriteLine(token[11]);
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,如果您像这样阅读它们,您可以随时将它们添加到
HashSet
中(假设是 .NET 3.5):或者,将您的代码视为 LINQ 查询:
Well, if you're reading them like this you could just add them to a
HashSet<string>
as you go (assuming .NET 3.5):Alternatively, think of your code as a LINQ query: