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. Replacing items within a string

Replacing items within a string

Scheduled Pinned Locked Moved C#
tutorialannouncement
13 Posts 4 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.
  • G Offline
    G Offline
    Goaty65109
    wrote on last edited by
    #1

    Hello CP! Alright, I thought this would be simple, but apparently I don't know what I am doing. I have a Rich Text Box that populates upon form load. There are fields that are meant to be updated by text boxes on this form. Here's a snip of the form_load:

        private void Form1\_Load(object sender, EventArgs e)
        {
            string cifTime = timeCIF.Text;
            string chkCIF = "N";
            emailBody.Text = "Example\\nCIF = " + chkCIF + "\\nTIME = " + timeCIF.Text;
        }
    

    So what happens here is the form opens and populates a rich text box with: Example CIF = N TIME = Now, what I need to have a button do is update just the items I have defined, in this case chkCIF (a check box) and timeCIF.Text (a textbox). I have a button that's called update, and here's what it looks like.

        private void updateButton\_Click(object sender, EventArgs e)
        {
            //There is a combo box on this form and example is the first option
            if (comboInst.Text == "Example")
            {
                //Here is where the check box verification is, if it is true it should change the N to Y
                if (checkCIF.Checked == true)
                {
                    //This part needs to update the current contents of the email body. Obviously, this isn't working for me so I commented out some examples I tried...
                    //string s = emailBody.Text;
                    //string chkCIF = "Y";
    
                    //s = s.Replace("N", chkCIF);
    
                    string theString = emailBody.Text;
                    theString.Remove(3, 2).Insert(3, "Y");
                }
            }
    

    And there you have it. I just need to be able to update certain defined words from a string with a click of a button. Thanks in advance :)

    L 2 Replies Last reply
    0
    • G Goaty65109

      Hello CP! Alright, I thought this would be simple, but apparently I don't know what I am doing. I have a Rich Text Box that populates upon form load. There are fields that are meant to be updated by text boxes on this form. Here's a snip of the form_load:

          private void Form1\_Load(object sender, EventArgs e)
          {
              string cifTime = timeCIF.Text;
              string chkCIF = "N";
              emailBody.Text = "Example\\nCIF = " + chkCIF + "\\nTIME = " + timeCIF.Text;
          }
      

      So what happens here is the form opens and populates a rich text box with: Example CIF = N TIME = Now, what I need to have a button do is update just the items I have defined, in this case chkCIF (a check box) and timeCIF.Text (a textbox). I have a button that's called update, and here's what it looks like.

          private void updateButton\_Click(object sender, EventArgs e)
          {
              //There is a combo box on this form and example is the first option
              if (comboInst.Text == "Example")
              {
                  //Here is where the check box verification is, if it is true it should change the N to Y
                  if (checkCIF.Checked == true)
                  {
                      //This part needs to update the current contents of the email body. Obviously, this isn't working for me so I commented out some examples I tried...
                      //string s = emailBody.Text;
                      //string chkCIF = "Y";
      
                      //s = s.Replace("N", chkCIF);
      
                      string theString = emailBody.Text;
                      theString.Remove(3, 2).Insert(3, "Y");
                  }
              }
      

      And there you have it. I just need to be able to update certain defined words from a string with a click of a button. Thanks in advance :)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Goaty65109 wrote:

      update certain defined

      Okay.... you are replacing/removing the text but then throwing the result away. You need to set your emailBody.Text back to the string that you have updated. emailBody.Text = theString.Remove(3, 2).Insert(3, "Y");

      G 1 Reply Last reply
      0
      • L Lost User

        Goaty65109 wrote:

        update certain defined

        Okay.... you are replacing/removing the text but then throwing the result away. You need to set your emailBody.Text back to the string that you have updated. emailBody.Text = theString.Remove(3, 2).Insert(3, "Y");

        G Offline
        G Offline
        Goaty65109
        wrote on last edited by
        #3

        Aha! I knew I was missing something so simple... I am running into my next problem... That removes items in position 3, over 2 spaces. How do I tell it to do ROW 2, position 3, over 2 spaces? And thank you so much for the speedy response.

        L B 2 Replies Last reply
        0
        • G Goaty65109

          Aha! I knew I was missing something so simple... I am running into my next problem... That removes items in position 3, over 2 spaces. How do I tell it to do ROW 2, position 3, over 2 spaces? And thank you so much for the speedy response.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Goaty65109 wrote:

          ROW 2

          I don't know too much about it but I do not believe that the stock rich text box supports the concepts of rows.... A cheap easy way to do what you want would be to split your string (Split function) replace the text in the resulting array and then rejoining the array back into a string. Easier than is sounds. Or you can replace your stock control with something like Line Numbers for RichText Control in C#[^]

          G 1 Reply Last reply
          0
          • G Goaty65109

            Aha! I knew I was missing something so simple... I am running into my next problem... That removes items in position 3, over 2 spaces. How do I tell it to do ROW 2, position 3, over 2 spaces? And thank you so much for the speedy response.

            B Offline
            B Offline
            Bogza Anton
            wrote on last edited by
            #5

            emailBody.Text = emailBody.Lines[1].Remove(3, 2).Insert(3, "Y");

            G 1 Reply Last reply
            0
            • L Lost User

              Goaty65109 wrote:

              ROW 2

              I don't know too much about it but I do not believe that the stock rich text box supports the concepts of rows.... A cheap easy way to do what you want would be to split your string (Split function) replace the text in the resulting array and then rejoining the array back into a string. Easier than is sounds. Or you can replace your stock control with something like Line Numbers for RichText Control in C#[^]

              G Offline
              G Offline
              Goaty65109
              wrote on last edited by
              #6

              Hey Phantom, I understand what you're saying there and if a rich text box isn't the best way I think I can work backwards on what I want to do then. How about writing the information to a text file, then calling it to the text box once it's updated in the text file? Here is the "flow" of things: Form loads and a .txt file is generated as a template. User checks the box which makes the N turn into a Y in the text file. User inputs the time into a text box. User clicks the "Update Form" button which saves the text document and then pulls the entire document into the rich text box revealing the changes. Does that sound like a better option? This just creates a new situation I have yet to attempt, but is on my "to learn" list, writing information to a specified position in a text file/reading from a text file. Thank you for your response :)

              S L 2 Replies Last reply
              0
              • B Bogza Anton

                emailBody.Text = emailBody.Lines[1].Remove(3, 2).Insert(3, "Y");

                G Offline
                G Offline
                Goaty65109
                wrote on last edited by
                #7

                Hi Rikki! I tried this method and it appears that this will clear the emailBody and replaces it with just the first line, and replaces the third position with Y. So this Example Blah = N turns to this ExYmple I just want this if possible Example Blah = Y Haha, sorry for my illiteracy, I am trying to be as specific as I can with the knowledge I have. :P

                B 1 Reply Last reply
                0
                • G Goaty65109

                  Hi Rikki! I tried this method and it appears that this will clear the emailBody and replaces it with just the first line, and replaces the third position with Y. So this Example Blah = N turns to this ExYmple I just want this if possible Example Blah = Y Haha, sorry for my illiteracy, I am trying to be as specific as I can with the knowledge I have. :P

                  B Offline
                  B Offline
                  Bogza Anton
                  wrote on last edited by
                  #8

                  For example:

                  private void SomeForm_Load(object sender, EventArgs e)
                  {
                  string cifTime = timeCIF.Text;
                  string chkCIF = "N";
                  emailBody.Text = "Example\nCIF = " + chkCIF + "\nTIME = " + timeCIF.Text;
                  }

                  private void SomeBtn_Click(object sender, EventArgs e)
                  {
                  string[] sLines = emailBody.Lines;
                  sLines[1] = emailBody.Lines[1].Remove(6, 1).Insert(6, "Y");
                  emailBody.Lines = sLines;
                  }

                  1 Reply Last reply
                  0
                  • G Goaty65109

                    Hello CP! Alright, I thought this would be simple, but apparently I don't know what I am doing. I have a Rich Text Box that populates upon form load. There are fields that are meant to be updated by text boxes on this form. Here's a snip of the form_load:

                        private void Form1\_Load(object sender, EventArgs e)
                        {
                            string cifTime = timeCIF.Text;
                            string chkCIF = "N";
                            emailBody.Text = "Example\\nCIF = " + chkCIF + "\\nTIME = " + timeCIF.Text;
                        }
                    

                    So what happens here is the form opens and populates a rich text box with: Example CIF = N TIME = Now, what I need to have a button do is update just the items I have defined, in this case chkCIF (a check box) and timeCIF.Text (a textbox). I have a button that's called update, and here's what it looks like.

                        private void updateButton\_Click(object sender, EventArgs e)
                        {
                            //There is a combo box on this form and example is the first option
                            if (comboInst.Text == "Example")
                            {
                                //Here is where the check box verification is, if it is true it should change the N to Y
                                if (checkCIF.Checked == true)
                                {
                                    //This part needs to update the current contents of the email body. Obviously, this isn't working for me so I commented out some examples I tried...
                                    //string s = emailBody.Text;
                                    //string chkCIF = "Y";
                    
                                    //s = s.Replace("N", chkCIF);
                    
                                    string theString = emailBody.Text;
                                    theString.Remove(3, 2).Insert(3, "Y");
                                }
                            }
                    

                    And there you have it. I just need to be able to update certain defined words from a string with a click of a button. Thanks in advance :)

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Why not keep all the individual pieces of text as separate variables and then rebuild the RichTextBox[^] using its methods, whenever an item changes? This should make it much easier to add or remove specific items in the future.

                    Use the best guess

                    L 1 Reply Last reply
                    0
                    • G Goaty65109

                      Hey Phantom, I understand what you're saying there and if a rich text box isn't the best way I think I can work backwards on what I want to do then. How about writing the information to a text file, then calling it to the text box once it's updated in the text file? Here is the "flow" of things: Form loads and a .txt file is generated as a template. User checks the box which makes the N turn into a Y in the text file. User inputs the time into a text box. User clicks the "Update Form" button which saves the text document and then pulls the entire document into the rich text box revealing the changes. Does that sound like a better option? This just creates a new situation I have yet to attempt, but is on my "to learn" list, writing information to a specified position in a text file/reading from a text file. Thank you for your response :)

                      S Offline
                      S Offline
                      SoMad
                      wrote on last edited by
                      #10

                      No, please don't bring a text file into this. It will not make it any easier to replace your substrings and you will just be complicating matters. See Richard's answer below, it seems like a much better solution. Soren Madsen

                      "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

                      1 Reply Last reply
                      0
                      • G Goaty65109

                        Hey Phantom, I understand what you're saying there and if a rich text box isn't the best way I think I can work backwards on what I want to do then. How about writing the information to a text file, then calling it to the text box once it's updated in the text file? Here is the "flow" of things: Form loads and a .txt file is generated as a template. User checks the box which makes the N turn into a Y in the text file. User inputs the time into a text box. User clicks the "Update Form" button which saves the text document and then pulls the entire document into the rich text box revealing the changes. Does that sound like a better option? This just creates a new situation I have yet to attempt, but is on my "to learn" list, writing information to a specified position in a text file/reading from a text file. Thank you for your response :)

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        Goaty65109 wrote:

                        a text file

                        A text file would just add unnecessary complexity IMO. I would probably do this just as Richard suggested.

                        1 Reply Last reply
                        0
                        • L Lost User

                          Why not keep all the individual pieces of text as separate variables and then rebuild the RichTextBox[^] using its methods, whenever an item changes? This should make it much easier to add or remove specific items in the future.

                          Use the best guess

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          Good idea

                          L 1 Reply Last reply
                          0
                          • L Lost User

                            Good idea

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #13

                            Thanks.

                            Use the best guess

                            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