0

所以我有这样的结构来搜索文本中的单词

 FileStream fs = new FileStream(fileName, FileMode.Open);
 StreamReader sr = new StreamReader(fs);
 fullText = sr.ReadToEnd();
 string[] arr = fullTetx.Split(' ');
 for (int i = 0; i < arr.Length; i++)//
 {
       if (arr[i].Trim() == "LOAD")
           Console.WriteLine(arr[i].Trim());
 }
 sr.Close();
 fs.Close();

我必须让所有类似的词都忽略 Linq 的大小写。

例如:

负载(绘图)= SET 4 = THRU 16,34 THRU 37, 48 THRU 53,61 FORCE(PLOT,CORNER)ds STRESS(PLOT,CORNER)妈妈爸爸 SPC = 1 负载 = 1 负载,负载。

我必须得到:

负载 负载 负载

4

4 回答 4

4

我不确定我是否理解 LOAD 的来源。它是硬编码的吗?如果是,那么类似:

foreach(var word in arr.Where(w => w.ToUpper() == "LOAD"))
    Console.WriteLine(word);
于 2013-03-06T10:27:09.143 回答
2
var matches = Regex.Matches(fullTetx, @"load", RegexOptions.IgnoreCase);
于 2013-03-06T10:29:52.493 回答
2

试试这个

    var result = arr.Where(x => string.Equals(x, "LOAD",StringComparison.OrdinalIgnoreCase)).ToList();
于 2013-03-06T10:28:26.770 回答
0

或者怎么样:

// If you want the lines that contain "LOAD"
var loads = File.ReadLines(fileName).
            SelectMany (l => l.Split(' ')).
            Where (s => s.ToUpperInvariant().Contains("LOAD"));

foreach(var s in loads)
{
    Console.WriteLine(s);
}

// If you just want instances of "LOAD"
var loads = File.ReadLines(fileName).
            SelectMany (l => l.Split(' ')).
            Where (s => s.ToUpperInvariant() == "LOAD");

foreach(var s in loads)
{
    Console.WriteLine(s);
}
于 2013-03-06T10:49:14.820 回答