简体   繁体   English

字符串和首字母索引的索引

[英]Index of string and first letter call

If i write 'a' in the run time , the program should display all the name starting with 'a' stored in a text file . 如果我在运行时写了'a' ,则程序应显示存储在文本文件中的所有以'a'开头的名称。

Whenever i write any letter, the names starting with that letter should be displayed. 每当我写任何字母时,都应显示以该字母开头的名称。

string contents = File.ReadAllText("Data.txt");
foreach (var line in File.ReadLines("Data.txt")) ;

What I assume you mean is that you want to select only the lines from your fine (that each line represents a name) that begin with a given input (for example a ) 我假设您的意思是,您只想从给定输入(例如a )开头的行(每行代表一个名称)中选择行。

string input = Console.ReadLine();

var result = File.ReadLines("data.txt").Where(line => line.StartsWith(input)).ToList();

to display all the names in one blow you can use: 一键显示所有名称,您可以使用:

Console.WriteLine(String.Join("\n", result));

In the case each line in your file contains more than one name then you can split it like this: (For this example the way to separate between names is , ): 如果文件中的每一行包含多个名称,则可以按如下所示拆分它:(在本示例中,名称之间的分隔方法是, ):

string input = Console.ReadLine();

var result = File.ReadLines("data.xml")
                 .SelectMany(line => line.Split(','))
                 .Select(name => name.Trim())
                 .Where(line => line.StartsWith(input)).ToList();   

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

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