给出一串n行,如:
启动学生 - 主体 - 最终学生 - 启动学生马克 - 端学生为 -start-student-total-end-student-students of students-students-details-percentage-end-student-details -start-student-subject-end-student -start-student-marks-end-student- -start-学生总端学生级 - 开始 - 学生百分比端-学生 -
我的方法:
string s = File.ReadAllText(@"D:\Data.txt");
int start = s.IndexOf("start-student") + 1;
int end = s.IndexOf("end-student", start);
string result = s.Substring(start, end - start);
Console.WriteLine(result);
我只得到一个字符串:
“受试者”
先谢谢
答案 0 :(得分:2)
您可以使用正则表达式解决此问题:
using System;
using System.IO;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string s = File.ReadAllText(@"D:\Data.txt");
var matches = Regex.Matches(s, "(?<=start-student)((?!end-student).)*");
foreach(var m in matches)
{
Console.Out.WriteLine(m);
}
}
}
可生产
- 主体 -
-marks -
-total -
-subject -
-marks -
-total -
-percentage -
答案 1 :(得分:0)
string s = File.ReadAllText(@"D:\SampleXML.txt");
Regex r = new Regex(@"start-student-(.+?)-end-student");
MatchCollection mc = r.Matches(s);
string[] c = new string[mc.Count];
for (int i = 0; i < mc.Count; i++)
{
c[i] = mc[i].Groups[0].Value;
}
string result = string.Join("\n", c);
Console.WriteLine(result);