获取c#文本行的前4个单词

本文关键字:4个 单词 文本 获取 | 更新日期: 2023-09-27 18:11:27

我有这个文本文件,其中包含如下字符串行。

iPhone 4S White 16GB MC920, MC924, MD237, MD239, MD277, MD378, MD866, ME805
iPhone 4S White 32GB MC921, MD244, MD279, MD380, MD246
iPhone 4S White 64GB MD260, MD271, MD272, MD281, MD382

我想实现的是,我想读取行并识别具有特定型号(以M开头)的行

例如

如果型号为MD378,我想读取行并找到具有型号的行,然后从该行获得前4个单词。这意味着我应该得到下面的单词。

iPhone4 s白色16 gb

我使用下面的代码读取文本文件

var fileStream = new FileStream(Directory.GetCurrentDirectory() + @"'DvDB.ADB", FileMode.Open, FileAccess.Read);
        using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
        {
            string line;
            while ((line = streamReader.ReadLine()) != null)
            {
                if (line.Contains(""+Model_Number))
                {
                    //Need to perform the processing here.
                }
            }
        }
如果我能知道如何做到这一点,我将不胜感激。

谢谢。

获取c#文本行的前4个单词

取一行的前四个字

var firstFourWords = streamReader.ReadLine()
    .Split(' ')    // Split line by whitespace
    .Take(4)       // Take first four words from the array
    .ToList();     // Optionally convert to List<string>

假设它总是4个单词,每个单词由空格字符分隔,您可以使用string.split来实现这一点。

    var fileStream = new FileStream(Directory.GetCurrentDirectory() + @"'DvDB.ADB", FileMode.Open, FileAccess.Read);
    using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
    {
        string line;
        while ((line = streamReader.ReadLine()) != null)
        {
            if (line.Contains(""+Model_Number))
            {
                //Need to perform the processing here.
                string[] lineArray = line.Split(' ').Take(4).ToArray();
                //Each word is assumed to be separated by a space character
                //Array now contains the first 4 words from the line.
            }
        }
    }

有一种方法:

var fileStream = new FileStream(Directory.GetCurrentDirectory() + @"'DvDB.ADB", FileMode.Open, FileAccess.Read);
        using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
        {
            string line;
            while ((line = streamReader.ReadLine()) != null)
            {
                if (line.Contains(""+Model_Number))
                {
                   var arr = line.split(' ');
                   Array.Resize(ref arr, 4);
                   string 4words = string.Join(" ", arr);
                }
            }
        }

您的代码几乎做到了这一点-它逐行读取文件并识别包含子字符串的行,您所需要做的就是最后一位。

最简洁的方法是在空格上使用String.Split,然后连接前4个非空元素-但是如果一行很长,那么String.Split就不是最优的(因为它将分配超过第4个单词的新字符串)。

return
    String.Join(
        line.Split( null, StringSplitOptions.RemoveEmptyEntries )
        .Take( 4 )
    ), " " );
  • line.Split( null, RemoveEmptyEntries )将在每个空白字符上分割字符串并返回String[],尽管这对于具有许多元素的长字符串是低效的。
  • String[]也是IEnumerable<String>,因此它可以与Linq的.Take(n)扩展方法一起使用,以返回由前4个(或更少)元素组成的IEnumerable<String>
  • 最后,String.Join( IEnumerable<String>, String )用于将前4个单词连接回一个字符串。

更有效的方法是扫描行中第四个换行符,并相应地生成一个子字符串:

int word4Idx = -1;
int word = 0;
bool inWS = true;
for(int i = 0; i < line.Length && word < 4; i++) {
    if( inWS ) {
        if( !Char.IsWhitespace( line[i] ) ) {
            word++;
            word4IDx = i;
            inWS = false;
        }
    }
    else {
        inWS = Char.IsWhitespace( line[i] );
    }
}
if( word4Idx > -1 && word4Idx < line.Length ) return line.Substring( 0, word4Idx );
return line;