理想情况下,你不应该做字符串:
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”? –