我有相當多的問題,我的第一個問題是如何才能做一個簡單的LINQ查詢來匹配文件中的單詞?我不是很笨,但我還沒有理解我爲LINQ找到的文檔。如何使用LINQ匹配單詞
回答
必要創建一個新的WindowsForms應用程序和使用下面的代碼。
會帶您需要添加一個標籤,標籤控件,文本框和一個按鈕
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
namespace LinqTests
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public String[]
Content;
public String
Value;
private void button1_Click(object sender, EventArgs e)
{
Value = textBox1.Text;
OpenFileDialog ofile = new OpenFileDialog();
ofile.Title = "Open File";
ofile.Filter = "All Files (*.*)|*.*";
if (ofile.ShowDialog() == DialogResult.OK)
{
Content =
File.ReadAllLines(ofile.FileName);
IEnumerable<String> Query =
from instance in Content
where instance.Trim() == Value.Trim()
orderby instance
select instance;
foreach (String Item in Query)
label1.Text +=
Item + Environment.NewLine;
}
else Application.DoEvents();
ofile.Dispose();
}
}
}
我希望這有助於
嗯耶。這確實幫助我。謝謝 – user164203 2009-11-16 15:19:20
不錯的例子。請注意,它會查找等於「Value」('textBox1.Text')的行,而不是像你在q中所提到的那樣。用'instance.Trim()'將行更改爲'where instance.Trim()。Contains(Value)'或類似的東西。 – Abel 2009-11-16 15:24:31
謝謝你的修正阿貝爾。非常感激。 – 2009-11-16 15:40:23
下面是怎麼回事?
string yourFileContents = File.ReadAllText("c:/file.txt");
string foundWordOrNull = Regex.Split(yourFileContents, @"\w").FirstOrDefault(s => s == "someword");
(誰曾表示,C#不能簡練?)
特碼的工作方式是閱讀您的文件,將其分割成詞,然後返回它發現被稱爲someword
的第一個字。
編輯:從上面的評論被認爲是「不是LINQ」。雖然我不同意(見註釋),我認爲同樣的方法更LINQified例子在這裏;-)
string yourFileContents = File.ReadAllText("c:/file.txt");
var foundWords = from word in Regex.Split(yourFileContents, @"\w")
where word == "someword"
select word;
if(foundWords.Count() > 0)
// do something with the words found
下面是從MSDN的例子計算一個單詞出現在字符串(http://msdn.microsoft.com/en-us/library/bb546166.aspx)。
string text = ...;
string searchTerm = "data";
//Convert the string into an array of words
string[] source = text.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' },
StringSplitOptions.RemoveEmptyEntries);
// Create and execute the query. It executes immediately
// because a singleton value is produced.
// Use ToLowerInvariant to match "data" and "Data"
var matchQuery = from word in source
where word.ToLowerInvariant() == searchTerm.ToLowerInvariant()
select word;
// Count the matches.
int wordCount = matchQuery.Count();
Console.WriteLine("{0} occurrences(s) of the search term \"{1}\" were found.",
wordCount, searchTerm);
這裏是另一個關於從文本文件http://www.onedotnetway.com/tutorial-reading-a-text-file-using-linq/讀取數據的LINQ教程。
- 1. 如何使用OMetaJS匹配單詞?
- 2. 使用RegEx匹配相似單詞
- 3. 使用sed條件匹配單詞
- 4. 如何不匹配mod_rewrite中的單詞
- 5. 如何匹配完整的單詞?
- 6. grep匹配一個單詞,但不匹配另一個單詞
- 7. 使用Solr匹配包含相似單詞的單詞嗎?
- 8. 如何匹配包含非單詞字符的單詞?
- 9. 如何使用匹配javascript搜索特定的單詞?
- 10. 如何使用python匹配文本文件中的單詞?
- 11. 如何使用strpos匹配大部分單詞?
- 12. 如何匹配在PHP中使用preg_replace的俄語單詞?
- 13. 如何使用正則表達式匹配單詞?
- 14. 如何使用Lucene在單個詞語中找到匹配
- 15. 如何匹配單詞'開始......',但如果引用則不匹配?
- 16. LINQ查詢匹配多個詞
- 17. PHP匹配整個單詞
- 18. 匹配除單個單詞外的任何單詞
- 19. 使用Linq計算實體框架中的匹配詞彙
- 20. 只用LIKE匹配整個單詞?
- 21. 用於匹配單詞或單詞的正則表達式
- 22. 如何用reg exp匹配阿拉伯語單詞?
- 23. 如何在PHP中匹配具有通用前綴的單詞?
- 24. SphinxQL - 如何匹配單個字符串內的單詞
- 25. SQL LIKE - 使用括號(字符範圍)匹配匹配整個單詞
- 26. 如何使用正則表達式匹配單個單詞和單詞的組合?
- 27. 如何在Python中使用re.compile如果我只想匹配字母單詞
- 28. 如何使用sed來匹配某個單詞,然後用引號包裝該單詞?
- 29. 如何匹配除一個單詞之外的任何文本
- 30. 使用NSString找到匹配後的詞
你試過了什麼(如果有的話)? – ChrisF 2009-11-16 15:12:05