How to insert multiple values in string efficiently?
-
Hello! Can anyone help me in the following scenario: - We need to insert multple comma notations in a string (25 in total), at a predefined index number. - Currently we are 25 inserts as below: line = line.Insert(7, ","); line = line.Insert(15, ","); line = line.Insert(37, ","); - Is there any better way of doing that!!!? Thanks in advance! Adeel --
-
Hello! Can anyone help me in the following scenario: - We need to insert multple comma notations in a string (25 in total), at a predefined index number. - Currently we are 25 inserts as below: line = line.Insert(7, ","); line = line.Insert(15, ","); line = line.Insert(37, ","); - Is there any better way of doing that!!!? Thanks in advance! Adeel --
string comma = ","; int[] insertIndexes[25] = {7,15,37,...}; for(int i =0;i < insertIndexes.Length;i++) { line = line.insert(insertIndexes[i],comma); }
-
Hello! Can anyone help me in the following scenario: - We need to insert multple comma notations in a string (25 in total), at a predefined index number. - Currently we are 25 inserts as below: line = line.Insert(7, ","); line = line.Insert(15, ","); line = line.Insert(37, ","); - Is there any better way of doing that!!!? Thanks in advance! Adeel --
Each line = line.Insert(7, ","); will create a new string and copy all its characters. If that results in several hundred chars being copied overall, I recommend you use a StringBuilder, with sufficient initial capacity. Then set up the logic to copy part of the input, add a comma, copy next part, etc using nested for loops, and/or an array of constants. finally transform StringBuilder object back to string with ToString(). :)
Luc Pattyn [My Articles]
-
string comma = ","; int[] insertIndexes[25] = {7,15,37,...}; for(int i =0;i < insertIndexes.Length;i++) { line = line.insert(insertIndexes[i],comma); }
:)! actually i was curious if there is any replacement for insert!!!:)!
-
Hello! Can anyone help me in the following scenario: - We need to insert multple comma notations in a string (25 in total), at a predefined index number. - Currently we are 25 inserts as below: line = line.Insert(7, ","); line = line.Insert(15, ","); line = line.Insert(37, ","); - Is there any better way of doing that!!!? Thanks in advance! Adeel --
const int[] predefinedIndexes = new int[] {7, 15, 37}; const string notation = ","; int lineLength = line.Length. foreach(int index in predefinedIndexes) { // just for your safety ;) if(index < 0 || index >= lineLength) continue; line.Insert(index, notation); } Hope this helps...
-
Hello! Can anyone help me in the following scenario: - We need to insert multple comma notations in a string (25 in total), at a predefined index number. - Currently we are 25 inserts as below: line = line.Insert(7, ","); line = line.Insert(15, ","); line = line.Insert(37, ","); - Is there any better way of doing that!!!? Thanks in advance! Adeel --
You could try the following code:
private string insertChars(string str, char insertChar, int[] insertIndexes) { StringBuilder b = new StringBuilder(str.Length + insertIndexes.Length); int count = 0; for (int i = 0; i != str.Length;i++) { if (i == insertIndexes[count]) { b.Append(insertChar); if (++count==insertIndexes.Length) { b.Append(str.Substring(i,str.Length-i)); break; } } b.Append(str[i]); } return b.ToString(); }
For it to work, your index array must be sorted.