Searching for Specific Characters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Grrlurp
    New Member
    • Jan 2014
    • 3

    Searching for Specific Characters

    How can I search for a specific character within a string?
    For example say I want to search for the letter "n"
    and the string is "peanut"- How can search for it so I can remove the n?
    For example- If the letter n is found- it is removed. So
    Searching for "n"
    Word "peanuts"
    Word is found
    Turns to "peauts"
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You can use the replace function to that end.

    Comment

    • Luk3r
      Contributor
      • Jan 2014
      • 300

      #3
      Rabbit is right. Here's a very simple example to work from:
      Code:
      TextBox1.Text = TextBox1.Text.Replace("n", "")

      Comment

      • Grrlurp
        New Member
        • Jan 2014
        • 3

        #4
        But when I do that, it replaces all characters before the letter- and how will I replace the same letters?
        Example:
        Word=Elephant
        There are two e's how do I replace only one?
        Edit:
        Nvm I can determine what position it replaces from- but it still deletes all characters before.

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          I don't know what you're talking about when you say "replacing the characters before".

          And this is the first time you mentioned that you only want to replace the first occurrence.

          You didn't mention any of this in your original post, you will need to explain in more detail.

          Comment

          • Grrlurp
            New Member
            • Jan 2014
            • 3

            #6
            I found some difficulties while replacing some characters.
            This is how Replace works:
            replace(word, letter, replacement4let ter, position, howmanyiwanttor eplace)
            So if I have:
            Textbox1.text = "-----"
            When Button1 is clicked: Textbox1.text = replace(textbox 1.text, "-", "a", 2, 1)
            And in the end Textbox1.text becomes = "a---"
            -
            How do I make it so the letters before the position is not deleted? I want it so when button 1 is clicked I want textbox1.text to be "-a---", not "a---".
            I hope I explained clearly
            Thx

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              In that case, you can substring out the beginning of the string, and append the replace result to that.

              Comment

              Working...