C# 在C中获取特定字符串后的文本行#
我正在读取一个类似这样的文件的内容C# 在C中获取特定字符串后的文本行#,c#,C#,我正在读取一个类似这样的文件的内容 [1] Message=my string here EffDate=1/1/1995 DeactDate= Modified= Note= [2] Message=my other string here EffDate=1/1/1995 DeactDate= Modified= Note= string[] getMessages(string file) { List<string> messages = new List
[1]
Message=my string here
EffDate=1/1/1995
DeactDate=
Modified=
Note=
[2]
Message=my other string here
EffDate=1/1/1995
DeactDate=
Modified=
Note=
string[] getMessages(string file) {
List<string> messages = new List<string>();
string[] lines = file.ReadAllLines(); /* read the lines */
foreach(string line in lines) {
string[] parts = line.Split('=');
string contentPart = String.Join("=", parts.Skip(1).ToArray();
if(parts[0]=="Message")
messages.Add(contentPart);
}
return messages.ToArray();
}
我想获取Message=
之后的字符串,但仅限于行的末尾。我不想要任何日期、笔记或任何东西
我希望将这两条消息都放入字符串数组或类似的东西中。有什么办法吗
非常感谢您的帮助。您可以使用followin方法
我希望这是可以理解的。你可以这样做
[1]
Message=my string here
EffDate=1/1/1995
DeactDate=
Modified=
Note=
[2]
Message=my other string here
EffDate=1/1/1995
DeactDate=
Modified=
Note=
string[] getMessages(string file) {
List<string> messages = new List<string>();
string[] lines = file.ReadAllLines(); /* read the lines */
foreach(string line in lines) {
string[] parts = line.Split('=');
string contentPart = String.Join("=", parts.Skip(1).ToArray();
if(parts[0]=="Message")
messages.Add(contentPart);
}
return messages.ToArray();
}
string[]获取消息(字符串文件){
列表消息=新列表();
string[]lines=file.ReadAllLines();/*读取行*/
foreach(行中的字符串行){
string[]parts=line.Split('=');
string contentPart=string.Join(“=”,parts.Skip(1.ToArray();
如果(部分[0]=“消息”)
messages.Add(contentPart);
}
返回消息。ToArray();
}
尽管请记住,这不是一个非常优雅和有效的解决方案,但最好是解析整个文件,填充一个结构数组,然后对其进行操作
为了说明原因,我使用了
Split
而不是其他任何东西:当比较和尝试列出的不仅仅是消息时,如果您已经在读取文件,那么使用StartsWith
或Contains
将变得无效,因为您只需要将第一行存储在一个字符串中ch包含字符串消息
,然后使用=
拆分字符串并存储在列表中
string strFilePath = @"D:\Maraj\Work\Ajax.txt";
FileStream objFS = new FileStream(strFilePath, FileMode.Open);
List<string> list = new List<string>();
using (StreamReader Sr = new StreamReader(objFS))
{
while (Sr.ReadLine() != null)
{
string line = Sr.ReadLine();
if(line.Contains("Message=")
{
list.Add(line .Split('=')[1]);
}
}
objFS.Close();
}
string strFilePath=@“D:\Maraj\Work\Ajax.txt”;
FileStream objFS=newfilestream(strFilePath,FileMode.Open);
列表=新列表();
使用(StreamReader Sr=新StreamReader(objFS))
{
while(Sr.ReadLine()!=null)
{
字符串行=Sr.ReadLine();
if(line.Contains)(“Message=”)
{
list.Add(line.Split('=')[1]);
}
}
objFS.Close();
}
读课文中的所有行
对于每行,检查行的开头是否存在消息=
如果是,则使用Substring()方法获取消息
var messages=File.ReadAllLines(“M:\\M.txt”);
列表结果=新列表();
foreach(消息中的var msg)
{
var key=“Message=”;
如果(消息启动(键))
添加(msg.Substring(msg.IndexOf(key)+key.Length));
}
这是您需要的工作代码:
var d = File.ReadAllLines(filePath);
var t = d.Where(g => g.Contains("Message"));
string[] splited;
foreach (var item in t)
{
splited = item.Split(new string[] { "Message=" }, StringSplitOptions.None);
Console.WriteLine(splited[1]);
}
如果您有任何疑问,请告诉我。您只需要一个简单的循环:
string[] messages = File.ReadAllLines("data.txt"); //read all lines
for (int i = 1; i < messages.Length; i = i + 7) //just get the line containing "Message"
{
string message = messages[i].Split('=')[1];
//now you have the message, save it where ever you want.
}
string[]messages=File.ReadAllLines(“data.txt”);//读取所有行
for(inti=1;i
这就足够了:
string[] messages = File.ReadLines("fileName.txt")
.Where(line => line.StartsWith("Message="))
.Select(line => line.Split(new char[] { '=' }, 2)[1])
.ToArray();
您不能使用split on=,因为它可以是Message=之后字符串的一部分。有疑问的是,User is说过该行将包含“Message=”;\但是他没有说=在那一行只出现一次。为了清楚起见,请看一下我的答案。你不能使用split on=,因为它可以是Message=之后字符串的一部分。@MaheshChand没有说明。无论如何,我做了一个更正编辑。仍然,错了。而且,他没有提到=在那一行只出现一次。恕我直言ar,请看一看我的答案。@MaheshChand为什么错了?你知道可能有多个正确答案,而不仅仅是你的答案?如果文本像..Message=我的字符串,你的答案会失败,并且它也包含=并且它希望它成为结果,你不能使用split on=,因为它可以是Message=之后的字符串的一部分。你不能使用split on=s因为它可以是Message=.之后字符串的一部分。@MaheshChand:有没有用户提到过,=符号可以出现一次或多次?为什么你投了反对票?谢谢,非常有用的答案。我可以问一下为什么我需要调用拆分的的第一个索引吗?是第0个索引Message=
?是的。Message=将位于第0个索引,其余的是line将位于第一个索引。@WesThompson您的消息值是否包含任何=
?如果文本像..Message=my字符串,并且它也包含=并且它希望它位于result@MaheshChandOP提供的测试用例不包含任何=
,只包含消息LD考虑OP已经声明了什么,不应该和我们的假设一致。@ MaheshChand不应该因为你的假设而投票所有正确的答案。所有的答案都是正确的,不同的方法。