Reputation: 5355
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
.
Upvotes: 123
Views: 170727
Reputation: 60606
It always bugged me that there's no way to "remove a char from a string", only "string from a string".
I took a modified version of Alexei Bondarev's answer and behcmarked it against the standard string.Replace(string, "")
| Method | Mean | Error | StdDev | Gen 0 | Allocated |
|--------------------- |---------:|----------:|---------:|-------:|----------:|
| StringReplace | 57.28 ns | 14.701 ns | 0.806 ns | 0.0063 | 40 B |
| RemoveCharFromString | 51.62 ns | 4.604 ns | 0.252 ns | 0.0216 | 136 B |
As you can see the results are nearly identical, but the 'RemoveCharFromString' allocates 3X memory.
The moral is - stick to String.Replace(string, "")
For reference, this is the method I used:
public static string RemoveCharFromString(string input, char charItem)
{
int indexOfChar = input.IndexOf(charItem);
while (indexOfChar >= 0)
{
input = input.Remove(indexOfChar, 1);
indexOfChar = input.IndexOf(charItem, indexOfChar);
}
return input;
}
Upvotes: 6
Reputation: 768
It always bothered me that I can't use the String.Remove
method to get rid of instances of a string or character in a string so I usually add theses extension methods to my code base:
public static class StringExtensions
{
public static string Remove(this string str, string toBeRemoved)
{
return str.Replace(toBeRemoved, "");
}
public static string RemoveChar(this string str, char toBeRemoved)
{
return str.Replace(toBeRemoved.ToString(), "");
}
}
The one taking char
can't use overload semantics unfortunately since it will resolve to string.Remove(int startIndex)
since it is "closer"
This is of course purely esthetics, but I like it...
Upvotes: 2
Reputation: 11
I have a latin version of the code of Razvan Dumitru because us use a million indicator even. Offcourse I use a double replace :D
public static string CleanNumb(string numb)
{
foreach (char c in ".,'´")
numb = numb.Replace(c, ' ');
return numb.Replace(" ", "");
}
Upvotes: 1
Reputation: 822
If you want to replace a char in a string with an empty char that means you want to remove that char from a string, read the answer of R. Martinho Fernandes.
Here is an exemple of how to remove a char from a string (replace with an "Empty char"):
public static string RemoveCharFromString(string input, char charItem)
{
int indexOfChar = input.IndexOf(charItem);
if (indexOfChar >= 0)
{
input = input.Remove(indexOfChar, 1);
}
return input;
}
or this version that removes all recurrences of a char in a string :
public static string RemoveCharFromString(string input, char charItem)
{
int indexOfChar = input.IndexOf(charItem);
if (indexOfChar < 0)
{
return input;
}
return RemoveCharFromString(input.Remove(indexOfChar, 1), charItem);
}
Upvotes: 5
Reputation: 12452
If you are in a loop, let's say that you loop through a list of punctuation characters that you want to remove, you can do something like this:
private const string PunctuationChars = ".,!?$";
foreach (var word in words)
{
var word_modified = word;
var modified = false;
foreach (var punctuationChar in PunctuationChars)
{
if (word.IndexOf(punctuationChar) > 0)
{
modified = true;
word_modified = word_modified.Replace("" + punctuationChar, "");
}
}
//////////MORE CODE
}
The trick being the following:
word_modified.Replace("" + punctuationChar, "");
Upvotes: 1
Reputation: 234404
Since the other answers here, even though correct, do not explicitly address your initial doubts, I'll do it.
If you call string.Replace(char oldChar, char newChar)
it will replace the occurrences of a character with another character. It is a one-for-one replacement. Because of this the length of the resulting string will be the same.
What you want is to remove the dashes, which, obviously, is not the same thing as replacing them with another character. You cannot replace it by "no character" because 1 character is always 1 character. That's why you need to use the overload that takes strings: strings can have different lengths. If you replace a string of length 1, with a string of length 0, the effect is that the dashes are gone, replaced by "nothing".
Upvotes: 74
Reputation: 8966
string val = "123-12-1234";
val = val.Replace("-", ""); // result: 123121234
Upvotes: 6
Reputation: 108947
You can use a different overload of Replace()
that takes string.
val = val.Replace("-", string.Empty)
Upvotes: 177