C# 当字符串出现时,遍历行并分成若干部分
我在遍历文件行时遇到了一个问题。此文件有数千行,它们如下所示:C# 当字符串出现时,遍历行并分成若干部分,c#,list,parsing,text,iteration,C#,List,Parsing,Text,Iteration,我在遍历文件行时遇到了一个问题。此文件有数千行,它们如下所示: # Tibia - graphical Multi-User-Dungeon # MonsterHomes File # Race X Y Z Radius Amount Regen. # ====== 0997,0988,09 ==================== 50 31927 31622 9 50 3 700 # ====== 0997,0989,09 ======
# Tibia - graphical Multi-User-Dungeon
# MonsterHomes File
# Race X Y Z Radius Amount Regen.
# ====== 0997,0988,09 ====================
50 31927 31622 9 50 3 700
# ====== 0997,0989,09 ====================
7 31931 31657 9 50 2 700
# ====== 0997,0990,09 ====================
5 31914 31691 9 50 4 600
50 31915 31687 9 50 2 650
# ====== 0997,0991,09 ====================
26 31930 31712 9 50 1 700
45 31925 31719 9 50 3 600
# ====== 0998,0986,10 ====================
49 31947 31564 10 50 1 900
# ====== 0998,0987,08 ====================
52 31958 31604 8 50 3 600
# ====== 0998,0987,10 ====================
49 31947 31586 10 50 2 900
# ====== 0998,0988,08 ====================
26 31963 31637 8 50 2 500
30 31961 31619 8 50 3 600
45 31945 31620 8 50 2 700
# ====== 0998,0988,09 ====================
5 31944 31618 9 50 1 600
5 31953 31623 9 50 4 700
5 31963 31631 9 50 1 600
7 31950 31624 9 50 1 650
36 31946 31630 9 50 1 600
# ====== 0998,0989,08 ====================
26 31954 31677 8 50 1 500
26 31948 31674 8 50 2 500
36 31942 31670 8 50 1 600
# ====== 0998,0989,09 ====================
19 31960 31676 9 50 4 700
# ====== 0998,0990,06 ====================
31 31950 31708 6 50 3 600
# ====== 0998,0990,08 ====================
我想做的是,每当#===
出现在行的开头时,就会创建一个名为Spawn的新对象。然后我将通过这一行和下面的所有行,直到下一个#====
并重复。首先我想跳过文件中的前5行
让我们以上面的文本为例。因此,第一个“繁殖”对象应该关注这一部分:
# ====== 0997,0988,09 ====================
50 31927 31622 9 50 3 700
下一个将是:
# ====== 0997,0989,09 ====================
7 31931 31657 9 50 2 700
第三点:
# ====== 0997,0990,09 ====================
5 31914 31691 9 50 4 600
50 31915 31687 9 50 2 650
它将继续这样下去
当行以#====
开头时,我已成功创建了一个新对象,但在下一次出现#=====
之前,我如何读取下面的行
static void Main(string[] args)
{
// Step 1. Read all lines in monster.db file - skip the first 5 lines
foreach (string line in File.ReadLines("monster.db").Skip(5))
{
if (line.StartsWith("# ======"))
{
// BEGIN SPAWN
Spawn spawn = new Spawn();
// Step 2. Get the sector file name
string filename = line.Replace(" ", "").Replace("=", "").Replace("#", "").Replace(",", "-") + ".sec";
spawn.SectorFile = filename;
// Step 3. Read the lines below until the next occurence of "# ======"
// And add the values to the object
spawn.xxxxx = the values
}
}
}
我不需要得到实际值的帮助。我只是想知道如何将这个文本文件分割成更小的部分。只要一行以开头,就会创建每个对象。我需要读那一行和下面的几行,直到下一次发生
如果不清楚,请告诉我。我尽量解释清楚
图片说明:
更新代码:
// List of spawns
public static List<Spawn> Spawns = new List<Spawn> { };
public static Spawn spawn = null;
// List of already used coordinates
public static List<Position> Positions = new List<Position> { };
public static Random rnd = new Random();
static void Main(string[] args)
{
// Step 1. Read all lines in monster.db file - skip the first 5 lines
foreach (string line in File.ReadLines("monster.db").Skip(5))
{
if (line.StartsWith("# ======"))
{
// BEGIN SPAWN
spawn = new Spawn();
// Step 2. Get the sector file name
string filename = line.Replace(" ", "").Replace("=", "").Replace("#", "").Replace(",", "-") + ".sec";
spawn.SectorFile = filename;
Spawns.Add(spawn);
}
// Continue to the next segment if no more monster lines
if (spawn == null)
continue;
// Step 3. Get the monsters
if (!line.Contains("#") && line.Length >= 10)
{
string replaceSpace = line.Replace(" ", "-");
string removeDuplicateDash = Regex.Replace(replaceSpace, @"\-+", "-");
string dashed = removeDuplicateDash.Substring(1);
spawn.MonsterId = Int32.Parse(dashed.Split('-').FirstOrDefault());
spawn.SpawnX = Int32.Parse(dashed.Split('-').Skip(1).FirstOrDefault());
spawn.SpawnY = Int32.Parse(dashed.Split('-').Skip(2).FirstOrDefault());
spawn.SpawnZ = Int32.Parse(dashed.Split('-').Skip(3).FirstOrDefault());
spawn.Radius = Int32.Parse(dashed.Split('-').Skip(4).FirstOrDefault());
spawn.Amount = Int32.Parse(dashed.Split('-').Skip(5).FirstOrDefault());
spawn.Regen = Int32.Parse(dashed.Split('-').Skip(6).FirstOrDefault());
}
}
// List all the spawns
foreach (Spawn s in Spawns)
{
Console.WriteLine($"{s.SectorFile} - {s.MonsterId}, [{s.SpawnX}, {s.SpawnY}, {s.SpawnZ}] - Amount: {s.Amount} - Radius: {s.Radius}");
}
}
//繁殖列表
公共静态列表生成=新列表{};
publicstaticspawn-Spawn=null;
//已使用坐标的列表
公共静态列表位置=新列表{};
公共静态随机rnd=新随机();
静态void Main(字符串[]参数)
{
//步骤1.读取monster.db文件中的所有行-跳过前5行
foreach(File.ReadLines(“monster.db”).Skip(5)中的字符串行)
{
if(第行开始使用(“#=======”)
{
//开始产卵
spawn=新的spawn();
//步骤2.获取扇区文件名
字符串文件名=line.Replace(“,”).Replace(“=”,”).Replace(“#“,”).Replace(“,”,“-”+”).sec”;
spawn.SectorFile=文件名;
繁殖。添加(繁殖);
}
//如果没有怪物线,继续下一段
if(spawn==null)
继续;
//第三步,抓住怪物
如果(!line.Contains(“#”)&&line.Length>=10)
{
字符串replaceSpace=line.Replace(“,“-”);
字符串removedUpplicatedDash=Regex.Replace(replaceSpace,@“\-+”,“-”;
字符串虚线=RemovedUpplicatedDash.子字符串(1);
spawn.MonsterId=Int32.Parse(虚线.Split('-').FirstOrDefault());
spawn.SpawnX=Int32.Parse(虚线.Split('-').Skip(1.FirstOrDefault());
spawn.SpawnY=Int32.Parse(虚线.Split('-').Skip(2.FirstOrDefault());
spawn.SpawnZ=Int32.Parse(虚线.Split('-').Skip(3.FirstOrDefault());
spawn.Radius=Int32.Parse(虚线.Split('-').Skip(4.FirstOrDefault());
spawn.Amount=Int32.Parse(虚线.Split('-').Skip(5.FirstOrDefault());
spawn.Regen=Int32.Parse(虚线.Split('-').Skip(6.FirstOrDefault());
}
}
//列出所有的繁殖
foreach(繁殖中的繁殖)
{
WriteLine($“{s.SectorFile}-{s.MonsterId},[{s.SpawnX},{s.SpawnY},{s.SpawnZ}]-数量:{s.Amount}-半径:{s.Radius}”);
}
}
现在我知道我真的明白OP想要什么了:)这就是
字符串头=null;
foreach(File.ReadLines(“monster.db”).Skip(5)中的字符串行)
{
if(第行开始使用(“#=======”)
{
字符串文件名=line.Replace(“,”).Replace(“=”,”).Replace(“#“,”).Replace(“,”,“-”+”).sec”;
spawnHeader=文件名;
}
if(spawnHeader==null)
继续;
如果(!line.Contains(“#”)&&line.Length>=10)
{
var spawn=新的spawn();
spawn.MonsterId=。。。。
...
spawn.SectorFile=spawnHeader;
繁殖。添加(繁殖);
}
}
尝试以下操作。代码已经过全面测试。不需要跳过行。您只需在行首输入#,以指示数据的起始位置:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication4
{
class Program
{
const string FILENAME = @"c:\temp\test.txt";
static void Main(string[] args)
{
List<Monster> monsters = new List<Monster>();
Monster monster = null;
StreamReader reader = new StreamReader(FILENAME);
string line = "";
string[] splitLine;
while ((line = reader.ReadLine()) != null)
{
line = line.Trim();
if (line.Length > 0)
{
if (line.StartsWith("#"))
{
if (line.StartsWith("# ======"))
{
splitLine = line.Split(new char[] { '#', ' ', '=' }, StringSplitOptions.RemoveEmptyEntries);
string id = splitLine[0];
monster = new Monster();
monster.id = id;
monsters.Add(monster);
}
}
else
{
splitLine = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (monster.homes == null) monster.homes = new List<Home>();
Home home = new Home()
{
Race = int.Parse(splitLine[0]),
X = int.Parse(splitLine[1]),
Y = int.Parse(splitLine[2]),
Z = int.Parse(splitLine[3]),
Radius = int.Parse(splitLine[4]),
Amount = int.Parse(splitLine[5]),
Regen = int.Parse(splitLine[6])
};
monster.homes.Add(home);
}
}
}
}
}
public class Monster
{
public string id { get; set; }
public List<Home> homes { get; set; }
}
public class Home
{
public int Race { get; set; }
public int X { get; set; }
public int Y { get; set; }
public int Z { get; set; }
public int Radius { get; set; }
public int Amount { get; set; }
public int Regen { get; set; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
命名空间控制台应用程序4
{
班级计划
{
常量字符串文件名=@“c:\temp\test.txt”;
静态void Main(字符串[]参数)
{
列出怪物=新建列表();
怪物=空;
StreamReader=新的StreamReader(文件名);
字符串行=”;
字符串[]分隔线;
而((line=reader.ReadLine())!=null)
{
line=line.Trim();
如果(直线长度>0)
{
if(第行开始,以“#”号填列)
{
if(第行开始使用(“#=======”)
{
splitLine=line.Split(新字符[]{'#','''''='},StringSplitOptions.RemoveEmptyEntries);
字符串id=分割线[0];
怪物=新怪物();
monster.id=id;
怪物。添加(怪物);
}
}
其他的
{
splitLine=line.Split(新字符[]{''},StringSplitOptions.RemoveEmptyEntries);
List<Spawn> Spawns = new List<Spawn>();
string curSectorFile = "";
// Step 1. Read all lines in monster.db file - skip the first 5 lines
foreach (string line in File.ReadLines("monster.db").Skip(5))
{
if (line.StartsWith("# ======"))
{
// Step 2. Get the sector file name
string filename = line.Replace(" ", "").Replace("=", "").Replace("#", "").Replace(",", "-") + ".sec";
curSectorFile = filename;
}
else if(curSectorFile != "")
{
Spawn sp = new Spawn();
sp.SectorFile = curSectorFile;
// parse "line"...
sp.xxx = yyy;
sp.xxx = yyy;
sp.xxx = yyy;
sp.xxx = yyy;
// add it to some kind of List<>?
Spawns.Add(sp);
}
}