public static string FormatInsertRemove(string template, params object[] values) { Comparts cp = CompileParts(template); //string tmp = string.Empty;// template; StringBuilder sb = new StringBuilder(template.Length * 2); var curpos = 0; for (int i = 0; i < cp.Len; i++) { sb.Append(template.Substring(curpos, cp.StartP[i] - curpos)); //tmp += template.Substring(curpos, cp.StartP[i] - curpos); sb.Append(values[cp.ParamId[i]]); //tmp += values[cp.ParamId[i]].ToString(); curpos = cp.LenP[i] + cp.StartP[i]; //tmp = tmp.Remove(cp.StartP[i], cp.LenP[i]).Insert(cp.StartP[i], values[cp.ParamId[i]].ToString()); //sb.Remove(cp.StartP[i], cp.LenP[i]); //sb.Insert(cp.StartP[i],values[cp.ParamId[i]]); } return(sb.ToString()); }
private static Comparts CompileParts(string template) { Comparts cf; if (m_cachecomp.TryGetValue(template, out cf)) return cf; cf = new Comparts(); int[] StartP = new int[10] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; int[] LenP = new int[10] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; int[] ParamId = new int[10] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; int currentPart = 0; bool insideBrackets = false; string tempParamId = string.Empty; int templatelength = template.Length; for (int i = 0; i < templatelength; i++) { char currentC = template[i]; if (currentC == '{') { insideBrackets = true; if (StartP.Length == currentPart) { Array.Resize(ref StartP, StartP.Length * 2); Array.Resize(ref LenP, LenP.Length * 2); Array.Resize(ref ParamId, ParamId.Length * 2); //nullify the array for (int z = currentPart; z < StartP.Length; z++) { StartP[z] = -1; LenP[z] = -1; ParamId[z] = -1; } } //parts[currentPart] = tempstr; //tempstr = ""; tempParamId = ""; StartP[currentPart] = i; continue; } else if (currentC == '}') { insideBrackets = false; LenP[currentPart] = i - StartP[currentPart]; ParamId[currentPart] = int.Parse(tempParamId); currentPart++; continue; } else { if (insideBrackets == true) tempParamId = tempParamId + currentC; } } cf.StartP = StartP; cf.LenP = LenP; cf.ParamId = ParamId; cf.Len = currentPart; m_cachecomp[template] = cf; return cf; }
private static Comparts CompileParts(string template) { Comparts cf; if (m_cachecomp.TryGetValue(template, out cf)) { return(cf); } cf = new Comparts(); int[] StartP = new int[10] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; int[] LenP = new int[10] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; int[] ParamId = new int[10] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; int currentPart = 0; bool insideBrackets = false; string tempParamId = string.Empty; int templatelength = template.Length; for (int i = 0; i < templatelength; i++) { char currentC = template[i]; if (currentC == '{') { insideBrackets = true; if (StartP.Length == currentPart) { Array.Resize(ref StartP, StartP.Length * 2); Array.Resize(ref LenP, LenP.Length * 2); Array.Resize(ref ParamId, ParamId.Length * 2); //nullify the array for (int z = currentPart; z < StartP.Length; z++) { StartP[z] = -1; LenP[z] = -1; ParamId[z] = -1; } } //parts[currentPart] = tempstr; //tempstr = ""; tempParamId = ""; StartP[currentPart] = i; continue; } else if (currentC == '}') { insideBrackets = false; LenP[currentPart] = i - StartP[currentPart]; ParamId[currentPart] = int.Parse(tempParamId); currentPart++; continue; } else { if (insideBrackets == true) { tempParamId = tempParamId + currentC; } } } cf.StartP = StartP; cf.LenP = LenP; cf.ParamId = ParamId; cf.Len = currentPart; m_cachecomp[template] = cf; return(cf); }