理想情況下,你不應該做字符串:
var databases = new Dictionary<string, string>();
databases["OINV"] = "Invoice";
databases["OPRQ"] = "Purchase Quotation";
databases["ORDR"] = "Order";
// ...
var str = "Some random text ORDR more text ORDR text OPRQ text OINV text ORDR";
var newstr = databases.Aggregate(str, (current, value) =>
current.Replace(value.Key, value.Value));
後者還可以,一旦你創建了詞典使用用生成的字符串替換 - 而是從翻譯的字符串生成它。因此,例如 - 不知道你實際上已經得到了什麼樣的代碼 - 你可以有:
private static readonly Dictionary<string, string> TableNameTranslations
= new Dictionary<string, string>
{
{ "ORDR", "Client order document" },
{ "OINV", "Invoice document" },
{ "OPRQ", "Purchase quotation" }
};
...
public string GetClientDocumentDisplayString(int clientId)
{
var tableNames = GetTableNamesForClient(clientId);
var translatedNames = tableNames.Select(t => TableNameTranslations[t]);
return $"Client with ID:{clientId} has documents: {string.Join(",", translatedNames)}";
}
private IList<string> GetTableNamesForClient(int clientId)
{
// Whatever your code needs, returning ORDR, OINV etc
}
的可能的複製[C#字符串替換字典(http://stackoverflow.com/questions/1231768/c-sharp -string-replace-with-dictionary) – NikolayKondratyev
是否需要定製而不需要重新編譯? –
您是否確實有需要替換的文本,或者您是否生成了「帶ID的客戶端:5634有文檔:OINV,ORDR」? –