尋找更好的算法/技術來替換字符串變量中的字符串。我必須遍歷未知數量的數據庫記錄,並且對於每一個記錄,我需要替換字符串變量中的一些文本。現在它看起來像這樣,但必須有一個更好的辦法:替換字符串中文本的最佳方法
using (eds ctx = new eds())
{
string targetText = "This is a sample string with words that will get replaced based on data pulled from the database";
List<parameter> lstParameters = ctx.ciParameters.ToList();
foreach (parameter in lstParameters)
{
string searchKey = parameter.searchKey;
string newValue = parameter.value;
targetText = targetText.Replace(searchKey, newValue);
}
}
從我的理解,因爲我在循環中寫targetText變量,一遍又一遍,這是不好的。然而,我不知道如何結構查找和替換...
感謝任何反饋。
你擔心性能或者某些替代值可能會被替換('「asd」.Replace(「a」,「s」)替換(「s」,「d」)' )? – Prusse