简体   繁体   English

替换字符串中的多个单词

[英]Replace multiple words in string

I have multiple words I want to replace with values, whats the best way to do this? 我有多个单词我想用值替换,最好的方法是什么?

Example: This is what I have done but it feels and looks so wrong 示例:这就是我所做的,但感觉和看起来都错了

string s ="Dear <Name>, your booking is confirmed for the <EventDate>";
string s1 = s.Replace("<Name>", client.FullName);
string s2 =s1.Replace("<EventDate>", event.EventDate.ToString());

txtMessage.Text = s2;

There has to be a better way? 一定有更好的方法?

thanks 谢谢

You could use String.Format . 你可以使用String.Format

string.Format("Dear {0}, your booking is confirmed for the {1}", 
   client.FullName, event.EventDate.ToString());

If you're planning on having a dynamic number of replacements, which could change at any time, and you want to make it a bit cleaner, you could always do something like this: 如果你计划有一个动态数量的替换,可以随时改变,并且你想让它更清洁,你总是可以这样做:

// Define name/value pairs to be replaced.
var replacements = new Dictionary<string,string>();
replacements.Add("<Name>", client.FullName);
replacements.Add("<EventDate>", event.EventDate.ToString());

// Replace
string s = "Dear <Name>, your booking is confirmed for the <EventDate>";
foreach (var replacement in replacements)
{
   s = s.Replace(replacement.Key, replacement.Value);
}

To build on George's answer, you could parse the message into tokens then build the message from the tokens. 要建立在George的答案之上,您可以将消息解析为令牌,然后从令牌构建消息。

If the template string was much larger and there are more tokens, this would be a tad more efficient as you are not rebuilding the entire message for each token replacement. 如果模板字符串更大并且有更多令牌,那么这将更有效,因为您没有为每个令牌替换重建整个消息。 Also, the generation of the tokens could be moved out into a Singleton so it is only done once. 此外,令牌的生成可以移出到Singleton中,因此只执行一次。

// Define name/value pairs to be replaced.
var replacements = new Dictionary<string, string>();
replacements.Add("<Name>", client.FullName);
replacements.Add("<EventDate>", event.EventDate.ToString());

string s = "Dear <Name>, your booking is confirmed for the <EventDate>";

// Parse the message into an array of tokens
Regex regex = new Regex("(<[^>]+>)");
string[] tokens = regex.Split(s);

// Re-build the new message from the tokens
var sb = new StringBuilder();
foreach (string token in tokens)
   sb.Append(replacements.ContainsKey(token) ? replacements[token] : token);
s = sb.ToString();

You can chain the Replace operations together: 您可以将Replace操作链接在一起:

s = s.Replace(...).Replace(...);

Note that you don't need to create other strings to do this. 请注意,您不需要创建其他字符串来执行此操作。

Using String.Format is the appropriate way, but only if you can change the original string to suit the brace formatting. 使用String.Format是合适的方法,但前提是您可以更改原始字符串以适应大括号格式。

When you do multiple replaces it's much more efficient to use StringBuilder instead of string. 当你进行多次替换时,使用StringBuilder而不是string更有效。 Otherwise replace function is making a copy of the string every time you run it, wasting time and memory. 否则,每次运行时,replace函数都会复制字符串,浪费时间和内存。

Try this code: 试试这段代码:

string MyString ="This is the First Post to Stack overflow";
MyString = MyString.Replace("the", "My").Replace("to", "to the");

Result: MyString ="This is My First Post to the Stack overflow"; 结果: MyString ="This is My First Post to the Stack overflow";

Use String.Format: 使用String.Format:

const string message = "Dear {0}, Please call {1} to get your {2} from {3}";
string name = "Bob";
string callName = "Alice";
string thingy = "Book";
string thingyKeeper = "Library";
string customMessage = string.Format(message, name, callName, thingy, thingyKeeper);

Improving on what @Evan said... 改进@Evan所说的......

string s ="Dear <Name>, your booking is confirmed for the <EventDate>";

string s1 = client.FullName;
string s2 = event.EventDate.ToString();

txtMessage.Text = s.Replace("<Name>", s1).Replace("EventDate", s2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM