Languages
[Edit]
EN

C#/.NET - remove characters from string

3 points
Created by:
Marcin
2447

In C#/.NET it is possible to remove string characters with String.Remove method.

1. Remove last character example

string text = "abc";
//string result = text.Remove(text.Length - 1, 1);
string result = text.Substring(0, text.Length - 1);
Console.WriteLine(text);

Output:

ab

2. Remove all characters after character position example

string text = "abc";
string result = text.Remove(1);
Console.WriteLine(result);

Output:

a

3. Remove all characters between character positions example

String text = "abc";
String result = text.Remove(1, 1);
Console.WriteLine(result);

Output:

ac

4. References

  1. String.Remove - Microsoft Docs
  2. String.Substring - Microsoft Docs
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.