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. Replace one character for another in a string

Replace one character for another in a string

Scheduled Pinned Locked Moved C#
helpquestion
4 Posts 3 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
    kani98
    wrote on last edited by
    #1

    Is there a way for me to check if a string contains a specific character and replace it with another character? I tried calling the Replace() method, but it doesn't seem to work. I want to check if a string contains the hyphen character, like in "Mike-Donald" and replace it with an underscore character to make it "Mike_Donald". Any help would be appreciated.

    D 1 Reply Last reply
    0
    • K kani98

      Is there a way for me to check if a string contains a specific character and replace it with another character? I tried calling the Replace() method, but it doesn't seem to work. I want to check if a string contains the hyphen character, like in "Mike-Donald" and replace it with an underscore character to make it "Mike_Donald". Any help would be appreciated.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Strings are immutable in the .NET Framework. Once created, they cannot be changed. How did you use the Replace method? Without seeing your code, it's impossible to tell you, with any certainty, what went wrong. But, the Replace method returns a new string object. It can NOT modify the current string:

      string newString = oldString.Replace(@"-", @"\_");
      

      Dave Kreskowiak Microsoft MVP - Visual Basic

      K 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Strings are immutable in the .NET Framework. Once created, they cannot be changed. How did you use the Replace method? Without seeing your code, it's impossible to tell you, with any certainty, what went wrong. But, the Replace method returns a new string object. It can NOT modify the current string:

        string newString = oldString.Replace(@"-", @"\_");
        

        Dave Kreskowiak Microsoft MVP - Visual Basic

        K Offline
        K Offline
        kani98
        wrote on last edited by
        #3

        The Replace method does work, it was my fault for not looking at the right variable. Is there some string method to check if the string contains a specific character like hyphen "-"?

        C 1 Reply Last reply
        0
        • K kani98

          The Replace method does work, it was my fault for not looking at the right variable. Is there some string method to check if the string contains a specific character like hyphen "-"?

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          The Contains method, I believe. If not, it's FirstIndexOf or something like that.

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          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