我是 C# 新手,需要幫助格式化此代碼我正在嘗試從剪貼板讀取特定單詞,然后將其輸出回剪貼板。我需要在字串串列搜索中添加無窮無盡的數字或單詞。
Text = Clipboard.GetText();
string Text = "Text to analyze for words, chair, table";
List<string> words = new List<string> { "chair", "table", "desk" };
var result = words.Where(i => Text.Contains(i)).ToList();
TextOut = Clipboard.SetText();
\\outputs “chair, table” to the clipboard
uj5u.com熱心網友回復:
您遇到的問題是這result是一個單詞串列,但您不能將串列放在剪貼板上。你必須把它變成一個字串。
您可以使用 Join 方法 (string.Join) 執行此操作,并指定要在單詞之間放置的內容,即逗號和空格:
//string Text = Clipboard.GetText();
string Text = "Text to analyze for words, chair, table";
List<string> words = new List<string> { "chair", "table", "desk" };
// No need for ToList - the enumerable will work with Join
IEnumerable<string> foundWords = words.Where(i => Text.Contains(i));
string result = string.Join(", ", foundWords);
Clipboard.SetText(result);
uj5u.com熱心網友回復:
你的代碼幾乎沒問題。
我認為您需要用空格或逗號分隔剪貼板中的單詞,但您需要找到一些方法來做到這一點。
接著 :
var Text = Clipboard.GetText();
//imagine you have words, chair, table
//you split to have an array containing
var arrayString = Text.Split(',')
List<string> wordsToSearch = new List<string> { "chair", "table", "desk" };
//you check your list
var result = wordswordsToSearch.Where(i => arrayString.Contains(i)).ToList();
//and set clipboard with matching content
var TextOut = Clipboard.SetText(result);
\\outputs “chair, table” to the clipboard
我不知道我的代碼是否有效,但這是我從您的需求中理解的想法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/382236.html
