在文本框中的特定单词后获取单词,然后将其复制到剪贴板 [英] Get word after a specific word in textbox and then copy it to clipboard

查看:90
本文介绍了在文本框中的特定单词后获取单词,然后将其复制到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们!



如何让我的程序在textbox1中的特定单词后找到一个单词,然后将其复制到剪贴板?特定单词后面的单词可以包含=#!?之类的字母顺便说一句。



我的尝试:



 Clipboard.SetText( ); 

解决方案

我写了这段代码:



 string FindWord(string specificWord)
{
string [] words = textBox1.Text.Split(''); //将textBox1的文本拆分为单词

for(var i = 0; i< words.Length; i ++)
{
//检查具体的字是否在单词数组和
//如果单词数组有空间至少另一个单词
if((words [i] == specificWord)&&(i<(words.Length - 1) ))
{
返回单词[i + 1];
}
}

返回;
}

void ProcessData()
{
string result = FindWord(test);

if(result!=)
{
try
{
Clipboard.SetText(result);
}
catch(异常x)
{
MessageBox.Show(错误复制+结果+到clipboard.\\\
\ n+ x);
}
}
}

void Button1Click(对象发送者,EventArgs e)
{
ProcessData();
}





到目前为止,它有效。


等等第二个:你没有说想要最后一个字,而是在特定字后面的字。我的代码正在做你刚才提出的问题。



例如如果test是特定的单词:

word1 word2 test word4 word5

我的代码将word4复制到剪贴板

Hey guys!

How can I make my program find a word after a specific word in textbox1 and then copy it to the clipboard? The word after the specific word can contain letters like "=#!?" btw.

What I have tried:

Clipboard.SetText("");

解决方案

I wrote this code:

string FindWord(string specificWord)
{
    string[] words = textBox1.Text.Split(' '); // split the textBox1's Text into words

    for (var i = 0; i < words.Length; i++)
    {
        // Check if the specificWord is in the words array and
        // if the words array has room for at least another word
        if ((words[i] == specificWord) && (i < (words.Length - 1)))
        {
            return words[i + 1];
        }
    }

    return "";
}

void ProcessData()
{
    string result = FindWord("test");

    if (result != "")
    {
        try
        {
            Clipboard.SetText(result);
        }
        catch (Exception x)
        {
            MessageBox.Show("Error copying " + result + " to clipboard.\n\n" + x);
        }
    }
}

void Button1Click(object sender, EventArgs e)
{
    ProcessData();
}



So far, it works.


Wait a second: you didn't say that want the last word, but the word immediately after the specific word. My code is doing what you asked in the first place.

e.g. If "test" is the specific word:
word1 word2 test word4 word5
My code will copy word4 to clipboard.


这篇关于在文本框中的特定单词后获取单词,然后将其复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆