Is there any function in C# that remove from string on specific index, for example
string s = "This is string"; s.RemoveAt(2);
s is now "Ths is string"
???
The most common approach to removing a character from a string at the specific index is using slicing. Here's a simple example that returns the new string with character at given index i removed from the string s .
Using 'str. replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.
The idea is to use the deleteCharAt() method of StringBuilder class to remove first and the last character of a string. The deleteCharAt() method accepts a parameter as an index of the character you want to remove.
Using translate(): translate() is another method that can be used to remove a character from a string in Python. translate() returns a string after removing the values passed in the table. Also, remember that to remove a character from a string using translate() you have to replace it with None and not "" .
string s = "This is string"; s = s.Remove(2, 1);
Output : Ths is string
1st parameter
is the starting index
from which you want to remove character and 2nd parameter is the number of character
you want to remove
As many others have said, there's a Remove
method. What the other posts haven't explained is that strings in C# are immutable -- you cannot change them.
When you call Remove
, it actually returns a new string; it does not modify the existing string. You'll need to make sure to grab the output of Remove
and assign it to a variable or return it... just calling Remove
on its own does not change the string.
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