如何 replace/change 文本框的特定单词与其他文本字段输入

How to replace/change specific words of a textbox with other text field input

我的问题是我有一个带有两个文本字段的 windows 表单。一个文本字段是隐藏的,另一个是可见的,隐藏的文本字段有文本 "ABCDEFG",我想要实现的是,当用户在可见文本字段中输入 "Hello",然后单击一个按钮,然后隐藏文本字段应该像 "ABCDHelloG" 一样更改其文本,其中只有中间的特定字母会更改为用户输入。这在 C# 中可能吗?如果您在想为什么有人会这样做,那么我会说,它用于触发和从资源中导出文件。如果您可能会问我为什么需要这样做,那么,对于一个项目来说,我一定会在这些条件下工作。

将隐藏文本定义为格式字符串。

string template = "ABCD{0}G"; // {0} is the placeholder for user value
string initial = string.Format(template, "EF"); // initial value of hidden field
// initial == "ABCDEFG"

然后使用字符串格式插入您的值:

string userValue = "Hello";
string result = string.Format(template, userValue);
// result == "ABCDHelloG"

如果必须连续多次使用,您必须确保模板没有被覆盖。