2011-01-21 79 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";

相關問題