Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the string Remove() method allow a char as a parameter?

Tags:

string

c#

char

People also ask

What is remove method in string?

In C#, Remove() method is a String Method. It is used for removing all the characters from the specified position of a string. If the length is not specified, then it will remove all the characters after specified position. This method can be overloaded by changing the number of arguments passed to it.

How do I replace a specific string in C#?

In C#, Replace() method is a string method. This method is used to replace all the specified Unicode characters or specified string from the current string object and returns a new modified string. This method can be overloaded by passing arguments to it.


you try to remove 'x' which is a declared as char, x is equal to 120

The .Remove only takes 2 parameters of type int the start and (optional) count to remove from the string.

If you pass a char, it will be converted to the integer representation. Meaning if you pass 'x' -> 120 is greater than the string's .Length and that's why it will throw this error!


Implicit conversion lets you compile this code since char can be converted to int and no explicit conversion is required. This will also compile and the answer will be 600 (120*5) -

        char c = 'x';
        int i = c;
        int j = 5;
        int answer = i * j;

From MSDN, implicit conversion is summarized as below -

Implicit conversion

As other's have stated you could use Replace or Remove with valid inputs.


There is no overload of Remove that takes a char, so the character is implicitly converted to an int and the Remove method tries to use it as an index into the string, which is way outside the string. That's why you get that runtime error instead of a compile time error saying that the parameter type is wrong.

To use Remove to remove part of a string, you first need to find where in the string that part is. Example:

var x = "tesx";
var x = x.Remove(x.IndexOf('x'), 1);

This will remove the first occurance of 'x' in the string. If there could be more than one occurance, and you want to remove all, using Replace is more efficient:

var x = "tesx".Replace("x", String.Empty);

Remove takes an int parameter for the index within the string to start removing characters, see msdn. The remove call must automatically convert the char to its ASCII integer index and try to remove the character at that index from the string, it is not trying to remove the character itself.

If you just want to remove any cases where x occurs in the string do:

"testx".Replace("x",string.Empty);

If you want to remove the first index of x in the string do:

var value = "testx1x2";
var newValue = value.Remove(value.IndexOf('x'), 1);

Since you are passing a char in the function and this value is getting converted to int at runtime hence you are getting the runtime error because the value of char at runtime is more than the length of the string.You may try like this:-

var x = "tesx";
var s = x.Remove(x.IndexOf('x'), 1);

or

var s = x.Replace("x",string.Empty);

.Remove takes the int as parameter. Remove takes two parameters. The first one is what position in your string you want to start at. (The count starts at zero.) The second parameter is how many characters you want to delete, starting from the position you specified.

On a side note:

From MSDN:

This method(.Remove) does not modify the value of the current instance. Instead, it returns a new string in which the number of characters specified by the count parameter have been removed. The characters are removed at the position specified by startIndex.