Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing char in string from specific index

Tags:

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"

???

like image 689
Dino Šatrović Avatar asked Dec 27 '14 16:12

Dino Šatrović


People also ask

How do I remove a character from a specific index?

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 .

How do you get rid of a certain character in a string?

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.

How do I remove a character from an index in a string in Java?

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.

How do I remove a specific item from a string in Python?

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 "" .


2 Answers

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

like image 72
Ali Akber Avatar answered Sep 30 '22 02:09

Ali Akber


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.

like image 33
Daniel Mann Avatar answered Sep 30 '22 02:09

Daniel Mann