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. .NET (Core and Framework)
  4. Count String Relpacements using Replace Function

Count String Relpacements using Replace Function

Scheduled Pinned Locked Moved .NET (Core and Framework)
4 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.
  • K Offline
    K Offline
    kermit888
    wrote on last edited by
    #1

    Hi, I successfully use the string.replace function. Newstring = oldstring.replace(“*”,”~”) Now I want to count the number of replacements made. Thanks in advance! Kermit

    L G W 3 Replies Last reply
    0
    • K kermit888

      Hi, I successfully use the string.replace function. Newstring = oldstring.replace(“*”,”~”) Now I want to count the number of replacements made. Thanks in advance! Kermit

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

      Hi, String.Replace() or any other method wont tell you, but you could try a loop

      int startPosition=0;
      int n;
      for (n=0; ;n++) {
      startPosition=oldstring.IndexOf(searchString, startPosition)+1;
      if (startPosition<=0) break;
      }

      before performing the Replace. :)

      Luc Pattyn

      1 Reply Last reply
      0
      • K kermit888

        Hi, I successfully use the string.replace function. Newstring = oldstring.replace(“*”,”~”) Now I want to count the number of replacements made. Thanks in advance! Kermit

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

        Perhaps it can be done using a RegEx replace and a delegate. That would give you the opportunity to do something for each replacement. A simple way (but not so effective if the string is large) is to create a string where you remove the character, and compare the lengths: int cnt = oldstring.Length - oldstring.Replace("*", "").Length;

        --- Year happy = new Year(2007);

        1 Reply Last reply
        0
        • K kermit888

          Hi, I successfully use the string.replace function. Newstring = oldstring.replace(“*”,”~”) Now I want to count the number of replacements made. Thanks in advance! Kermit

          W Offline
          W Offline
          Walter_H
          wrote on last edited by
          #4

          before doing the replacement you could use RegEx.Matches(InputString,SearchPattern).Count. BTW: RegEx.Replace is also a good method to do string-replacements. - walter

          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