如何从文件中分割我想要的文本?

时间:2022-01-10 06:56:19

Hi This is Wht I got in a file:

嗨这是我收到的文件:

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

---------------------------------------------------------------------------------

I just want to get the lines i.e text from the file.Like "B confident........waver". How do I do it ?

我只是想从文件中获取行,即文本。喜欢“B自信........摇摆”。我该怎么做 ?

I tried with splitting but I cant get it running.....:)

我尝试分裂,但我不能让它运行..... :)

4 个解决方案

#1


Read the file with a StreamReader and use the ReadLine method, that will read the file one line at a time.

使用StreamReader读取文件并使用ReadLine方法,该方法将一次读取一行文件。

using (StreamReader reader = File.OpenText(fileName))
{
    while (!reader.EndOfStream)
    {
        string line = reader.ReadLine();
        // do something with the line
    }
}

#2


The following will get you your lines into a string array:

以下内容将为您提供一个字符串数组:

string[] lines = File.ReadAllLines(pathToFile);

So:

lines[2];
lines[4];

Will get you those lines.

会得到你那些台词。

See the msdn documentation for ReadAllLines.

有关ReadAllLines的信息,请参阅msdn文档。

#3


Looks like every line in your sample that is not "valid" includes the text "+CGML". In that case, this should do the trick:

看起来样本中不是“有效”的每一行都包含文本“+ 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;
        }
    }
}

#4


Can use the streamReader to read in the lines and use a Regex to match certain lines...if there is a pattern you want to match.

可以使用streamReader读取行并使用正则表达式匹配某些行...如果有匹配的模式。

example:

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);
            }
    }
}

#1


Read the file with a StreamReader and use the ReadLine method, that will read the file one line at a time.

使用StreamReader读取文件并使用ReadLine方法,该方法将一次读取一行文件。

using (StreamReader reader = File.OpenText(fileName))
{
    while (!reader.EndOfStream)
    {
        string line = reader.ReadLine();
        // do something with the line
    }
}

#2


The following will get you your lines into a string array:

以下内容将为您提供一个字符串数组:

string[] lines = File.ReadAllLines(pathToFile);

So:

lines[2];
lines[4];

Will get you those lines.

会得到你那些台词。

See the msdn documentation for ReadAllLines.

有关ReadAllLines的信息,请参阅msdn文档。

#3


Looks like every line in your sample that is not "valid" includes the text "+CGML". In that case, this should do the trick:

看起来样本中不是“有效”的每一行都包含文本“+ 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;
        }
    }
}

#4


Can use the streamReader to read in the lines and use a Regex to match certain lines...if there is a pattern you want to match.

可以使用streamReader读取行并使用正则表达式匹配某些行...如果有匹配的模式。

example:

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);
            }
    }
}