用多个单词替换字符串中的多个单词

时间:2020-09-11 07:20:42

标签: c#

所以我要做的是列出我的名单

List<string> words = new List<string>();
            words.AddRange(new string[]{ "list of words", "i want to replace" })

现在我想从文本框中获取文本,并从“单词”列表中搜索单词,然后将所有单词都替换为相同的文本+一些东西,对不起,如果这令人困惑或解释不清,但是我做了最好,感谢所有帮助! :D

2 个答案:

答案 0 :(得分:0)

如果我理解的正确,那么您想要实现以下目标:

string textboxString = "Text from Textbox";
List<string> words = new List<string>();
words.Add("Text");

foreach(string word in words)
{
    textboxString = textboxString.Replace(word, word + " + something");
}

您也可以使用Regex.Replace();

答案 1 :(得分:0)

根据我对您问题的理解,您想在某些单词上添加一些内容。如果您希望根据单词更改某些内容,则需要一个词典(嗯...需要一个很强的单词)

    string textboxString = "Text from Textbox";
    Dictionary<string, string> words = new Dictionary<string, string>();
    words.Add("Text", "Something");
    words.Add("from", "SomethingElse");
    
    foreach (var key in words)
        textboxString = textboxString.Replace(key.Key, key.Key + key.Value);

这应该给您:

"TextSomething fromSomethingElse TextSomethingbox"
相关问题