首页 > 代码库 > 逐行读取txt文件,使用Linq与StreamReader的Readline方法
逐行读取txt文件,使用Linq与StreamReader的Readline方法
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 = http://www.mamicode.com/"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方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。