嗨这是我收到的文件:
AT+CMGL="ALL" +CMGL: 6123,"REC READ","+923315266206" B confident dat GOD can make a way wen der seems 2 b no way. Even wen your mind may waver, GOD is working bhind d scenes on yur behalf. Have a faith-filled day +CMGL: 6122,"REC READ","+923315266206" B confident dat GOD can make a way wen der seems 2 b no way. Even wen your mind may waver, GOD is working bhind d scenes on yur behalf. Have a faith-filled day ---------------------------------------------------------------------------------
我只是想从文件中获取行,即文本。喜欢“B自信........摇摆”。 我该怎么做?
我尝试分裂,但我无法让它运行.....:)
答案 0 :(得分:4)
使用StreamReader读取文件并使用ReadLine方法,该方法一次读取一行文件。
using (StreamReader reader = File.OpenText(fileName))
{
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
// do something with the line
}
}
答案 1 :(得分:3)
以下内容将为您提供一个字符串数组:
string[] lines = File.ReadAllLines(pathToFile);
所以:
lines[2];
lines[4];
会给你那些台词。
请参阅ReadAllLines的msdn文档。
答案 2 :(得分:2)
看起来样本中不是“有效”的每一行都包含文字"+CGML"
。在这种情况下,这应该可以解决问题:
public static IEnumerable<string> GetText(string filePath)
{
using (StreamReader sr = new StreamReader(filePath))
{
string line;
while ( (line = sr.ReadLine()) != null)
{
if (line.IndexOf("+CMGL") < 0) yield return line;
}
}
}
答案 3 :(得分:0)
可以使用streamReader读取行并使用正则表达式匹配某些行...如果有匹配的模式。
示例:
using System.IO;
using System.Text.RegularExpressions;
Regex pattern = new Regex("^B");
List<string> lines = new List<string>();
using (StreamReader reader = File.OpenText(fileName))
{
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
Match patternMatch = pattern.Match(blah);
if (patternMatch.Groups.Count > 0)
{
lines.Add(blah);
}
}
}