I have a string like this:
string val = "123-12-1234";
How can I replace the dashes using an empty string in C#.
I mean val.Replace(char oldChar, newChar);
What needs to go in oldChar
and newChar
.
You can use c[i]= '\0' or simply c[i] = (char) 0 . The null/empty char is simply a value of zero, but can also be represented as a character with an escaped zero.
String.Replace Method (String, String)Use String. Empty or null instead of "" since "" will create an object in the memory for each occurrences while others will reuse the same object.
The Java string replace() method will replace a character or substring with another character or string. The syntax for the replace() method is string_name. replace(old_string, new_string) with old_string being the substring you'd like to replace and new_string being the substring that will take its place.
An empty string ( "" ) consists of no characters followed by a single string terminator character - i.e. one character in total.
You can use a different overload of Replace()
that takes string.
val = val.Replace("-", string.Empty)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With