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