码迷,mamicode.com
首页 > 其他好文 > 详细

逐行读取txt文件,使用Linq与StreamReader的Readline方法

时间:2016-10-30 17:00:34      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:mat   har   read   sage   word   value   option   ring   readlines   

 

List<string[]> list = File.ReadLines("YourFile.txt")
.Select(r => r.TrimEnd(#))
.Select(line => line.Split(,))
.ToList();

or

List<string[]> list = File.ReadLines("YourFile.txt")
.Select(r => r.TrimEnd(#).Split(,))
.ToList();

File.ReadLines would read the file line by line.

.Select(r => r.TrimEnd(‘#‘)) would remove the # from end of the line
.Select(line => line.Split(‘,‘)) would split the line on comma and return an array of string items.
ToList() would give you a List<string[]> back.
 

using System;

public class Example
{
   public static void Main()
   {
      string[] separators = {",", ".", "!", "?", ";", ":", " "};
      string value = "The handsome, energetic, young dog was playing with his smaller, more lethargic litter mate.";
      string[] words = value.Split(separators, StringSplitOptions.RemoveEmptyEntries);
      foreach (var word in words)
         Console.WriteLine(word);
   }
}

 

StreamReader sr = new StreamReader(@monsterLocation);
            int searchId = monsterId;
            int actualId = 0;
            string name = "(Not found)";
            string[] details = null;
            string line = null;
            while ((line = sr.ReadLine()) != null)
            {
                line = line.Trim();
                if (line == "") continue;
                details = line.Split(\t);
                actualId = int.Parse(details[0]);
                if (actualId == searchId)
                {
                    name = details[2].Replace("\"", "");
                    break;
                }
            }
            sr.Close();
            Messagebox.shou("Result:" +name);
 

 

逐行读取txt文件,使用Linq与StreamReader的Readline方法

标签:mat   har   read   sage   word   value   option   ring   readlines   

原文地址:http://www.cnblogs.com/noteswiki/p/6013175.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!