Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a char in string with an Empty character in C#.NET

Tags:

c#

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.

like image 789
SaiBand Avatar asked Jun 16 '11 14:06

SaiBand


People also ask

How do you make a char empty?

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.

How do you replace occurrences with an empty string?

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.

How do I replace a character in a string?

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.

What is the empty string in C?

An empty string ( "" ) consists of no characters followed by a single string terminator character - i.e. one character in total.


1 Answers

You can use a different overload of Replace() that takes string.

val = val.Replace("-", string.Empty) 
like image 174
Bala R Avatar answered Nov 15 '22 06:11

Bala R