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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Removing selected text from a string

Removing selected text from a string

Scheduled Pinned Locked Moved C#
helpquestion
8 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.
  • U Offline
    U Offline
    User 2028214
    wrote on last edited by
    #1

    I'm trying to compare words entered in a textbox with a selection of stop-words, removing any stopwords that are found. Here's a snippet. string text = this.textBox1.Text; if(RemoveMe(s)) { int startIndex = text.IndexOf(s); int endIndex = s.Length-1; text = text.Remove(startIndex, startIndex+endIndex); } Problem is, every now and then startIndex get a value of -1... Kinda stumped here, what am I doing wrong?

    S L G 3 Replies Last reply
    0
    • U User 2028214

      I'm trying to compare words entered in a textbox with a selection of stop-words, removing any stopwords that are found. Here's a snippet. string text = this.textBox1.Text; if(RemoveMe(s)) { int startIndex = text.IndexOf(s); int endIndex = s.Length-1; text = text.Remove(startIndex, startIndex+endIndex); } Problem is, every now and then startIndex get a value of -1... Kinda stumped here, what am I doing wrong?

      S Offline
      S Offline
      Snowblind37
      wrote on last edited by
      #2

      When the iterator reaches the end it sets itself at -1. You need to re-set it at 0 after each string that you are finished with

      1 Reply Last reply
      0
      • U User 2028214

        I'm trying to compare words entered in a textbox with a selection of stop-words, removing any stopwords that are found. Here's a snippet. string text = this.textBox1.Text; if(RemoveMe(s)) { int startIndex = text.IndexOf(s); int endIndex = s.Length-1; text = text.Remove(startIndex, startIndex+endIndex); } Problem is, every now and then startIndex get a value of -1... Kinda stumped here, what am I doing wrong?

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

        int startIndex = text.IndexOf(s); int endIndex = s.Length-1; text = text.Remove(startIndex, s.Length); works fine for me. Remember the second parameter to "Remove" number of chars to delete, not the endIndex Can you give us some example strings you are trying to find in "text"?

        U 1 Reply Last reply
        0
        • U User 2028214

          I'm trying to compare words entered in a textbox with a selection of stop-words, removing any stopwords that are found. Here's a snippet. string text = this.textBox1.Text; if(RemoveMe(s)) { int startIndex = text.IndexOf(s); int endIndex = s.Length-1; text = text.Remove(startIndex, startIndex+endIndex); } Problem is, every now and then startIndex get a value of -1... Kinda stumped here, what am I doing wrong?

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          The IndexOf method returns -1 when the string is not found. You have to check if the word is even present in the string before you try to remove it. Also, your method is only removing the first occurance of the word. Why don't you use a Replace instead? Or why not use a regular expression? Then you can match the word boundaries, so that you don't accidentally remove a part of a word that matches another word. --- b { font-weight: normal; }

          U 1 Reply Last reply
          0
          • L Lost User

            int startIndex = text.IndexOf(s); int endIndex = s.Length-1; text = text.Remove(startIndex, s.Length); works fine for me. Remember the second parameter to "Remove" number of chars to delete, not the endIndex Can you give us some example strings you are trying to find in "text"?

            U Offline
            U Offline
            User 2028214
            wrote on last edited by
            #5

            hehe...thanks, would have had to be something that simple... I'm removing common words such as 'at' 'the' 'this' 'is' 'of' etc...

            1 Reply Last reply
            0
            • G Guffa

              The IndexOf method returns -1 when the string is not found. You have to check if the word is even present in the string before you try to remove it. Also, your method is only removing the first occurance of the word. Why don't you use a Replace instead? Or why not use a regular expression? Then you can match the word boundaries, so that you don't accidentally remove a part of a word that matches another word. --- b { font-weight: normal; }

              U Offline
              U Offline
              User 2028214
              wrote on last edited by
              #6

              I'd use regex but they always leaves me scratching my head somewhat puzzled :) Thanks for the suggestions, off to try it in the code now

              L 1 Reply Last reply
              0
              • U User 2028214

                I'd use regex but they always leaves me scratching my head somewhat puzzled :) Thanks for the suggestions, off to try it in the code now

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

                They're simple, really :) Just take yourself a week and learn it, it's worth a lot when dealing with strings.

                U 1 Reply Last reply
                0
                • L Lost User

                  They're simple, really :) Just take yourself a week and learn it, it's worth a lot when dealing with strings.

                  U Offline
                  U Offline
                  User 2028214
                  wrote on last edited by
                  #8

                  yeah, I've kinda put it off long enough :) thanks

                  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