2011-01-21 76 views
19

我有多个单词我想用值替换,最好的方法是什么?替换字符串中的多个单词

举例: 这是我做了什么,但感觉和看起来如此错误

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; 

必须有一个更好的办法?

感谢

+3

我建议更多的东西异国情调的替换字符串标记,如$ $ VAL $ $$而不是,因为有一天消息可能实际上需要具有类似XML的内容。 – 2011-01-21 21:17:42

回答

12

如果你打算在具有替代的动态数量,这可以在任何时间而改变,你想让它有点清洁,你总是可以做是这样的:

// 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); 
} 
23

你可以使用String.Format

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

+1,这也是为什么它是一个好主意:http://stackoverflow.com/questions/4671610/why-use-string-format/4671668#4671668 – 2011-01-21 20:48:23

+0

该文本是由用户指定的报告,是他们的所以他们知道他们不必编写客户端名称,系统将拉取对象并用值替换<>。 – David 2011-01-21 20:54:47

+1

格式将很好用,对于string.Format(“Dear {0},您的预订已确认{1}”, client.FullName,event.EventDate。的ToString()); 但是,文本是custome(由用户),所以你不知道文本将出现在哪里,所以你不能使用格式选项。 – David 2011-01-22 07:48:46

4

您可以链的替换操作起来:

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

请注意,您不需要创建其他的字符串来做到这一点。

使用String.Format是合适的方式,但前提是您可以更改原始字符串以适应大括号格式。

2

使用的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); 
10

要建立在乔治的回答,您可以解析消息成标记然后生成令牌的消息。

如果模板字符串大得多,并且有更多的令牌,这样会更有效一些,因为您不会为每个令牌替换重建整个消息。此外,代币的生成可以移出一个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(); 
3

当你做多的内容替换它更有效地使用StringBuilder,而不是字符串。否则,替换函数在每次运行时都会复制字符串,浪费时间和内存。

1

改进什么@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); 
2

试试这个代码:

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

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

相关问题