Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to insert multiple values in string efficiently?

How to insert multiple values in string efficiently?

Scheduled Pinned Locked Moved C#
databasehelptutorialquestion
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Adeel Chaudhry
    wrote on last edited by
    #1

    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 --

    R L A G 4 Replies Last reply
    0
    • A Adeel Chaudhry

      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 --

      R Offline
      R Offline
      Rob Graham
      wrote on last edited by
      #2

      string comma = ","; int[] insertIndexes[25] = {7,15,37,...}; for(int i =0;i < insertIndexes.Length;i++) { line = line.insert(insertIndexes[i],comma); }

      A 1 Reply Last reply
      0
      • A Adeel Chaudhry

        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 --

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        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]

        1 Reply Last reply
        0
        • R Rob Graham

          string comma = ","; int[] insertIndexes[25] = {7,15,37,...}; for(int i =0;i < insertIndexes.Length;i++) { line = line.insert(insertIndexes[i],comma); }

          A Offline
          A Offline
          Adeel Chaudhry
          wrote on last edited by
          #4

          :)! actually i was curious if there is any replacement for insert!!!:)!

          1 Reply Last reply
          0
          • A Adeel Chaudhry

            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 --

            A Offline
            A Offline
            AFSEKI
            wrote on last edited by
            #5

            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...

            1 Reply Last reply
            0
            • A Adeel Chaudhry

              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 --

              G Offline
              G Offline
              gumi_r msn com
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups