Removing char in a row from a specific index

Is there any function in C # that is removed from a string at a specific index, for example

string s = "This is string"; s.RemoveAt(2); 

s now "Ths is string"

???

+7
string c #
source share
5 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 the character, and the second parameter is the number of character that you want to remove

+9
source share

Like many others, there is a Remove method. Other posts have not explained that strings in C # are immutable - you cannot change them.

When you call Remove , it actually returns a new line ; it does not modify the existing row. You will need to make sure that you select the output of Remove and assign it to a variable or return it ... just calling Remove by itself will not change the line.

+5
source share

You can also use regular expressions.

 Console.WriteLine(Regex.Replace("This is string", @"(?<=^.{2}).", "")); 

This will remove the third character from the beginning.

Demo

+1
source share

There is a String.Remove method:

 s = s.Remove(2, 1); 
0
source share

You can write for your extension for this using the Remove method:

 public static class MyStringExtensions { public static string RemoveAt(this string s, int index) { return s.Remove(index, 1); } } 

using:

 string s = "This is string"; s = s.RemoveAt(2); 
0
source share

All Articles