Delete Characters from strings in C#

By May 15, 2016

Description:

Delete Characters from strings in CSharp

Preferencesoft

In this article, we'll see how to delete one or more characters from a string in C#.

Deleting a certain number of characters at a given position

The remove method of the String class directly performs this operation.

With a single parameter, the function removes all characters from the given position to the end. With two parameters, the method removes the given number of characters from a given position.

Example:

static void Main(string[] args)
        {
            string s = "123456789";
            Console.WriteLine(s.Remove(2));
            Console.WriteLine(s.Remove(3, 4));
            Console.ReadKey();
        }

This gives:

12

12389

How to delete characters in a string between two positions p1 and p2 (p1 p2)?

It uses the previous function providing as parameters position p1 and the number of characters p2- p1+1, the number of characters is equal to the difference plus one.

X Y 1 2 3 4 5 A B
p1 p2

Example:

static void Main(string[] args)
        {
            string s = "123456789";
            Console.WriteLine(s.Remove(2, 4-2+1)); //remove characters between 2 and 4
            Console.ReadKey();
        }

Result:

126789

How to remove the last n characters of a string?

Simply determine the start position since the number of characters is given. For this, we use the Length method of the String class giving the length of the string.

Note the length len of the string. The last character position is equal to len-1.

For the position of the first character to be deleted, simply step back of n-1 places. we obtain: len-1-(n-1)= len-n

X Y 1 2 3 4 5 6
len-n len-1 len

Example:

static void Main(string[] args)
        {
            string s = "123456789";
            int n = 4;
            Console.WriteLine(s.Remove(s.Length-n, n));
            Console.ReadKey();
        }

Result: 12345

How to remove n characters to the left and p characters to the right of the string?

The best is to extract the substring from the string s beginning at position n and having s.Length-n-p characters.

Example:

static void Main(string[] args)
        {
            string s = "123456789";
            int n = 2;
            int p = 3;
            Console.WriteLine(s.Substring(n,s.Length-n-p));
            Console.ReadKey();
        }

Deleting a particular character in a string

To delete a specific character in a string, just replace the string consisting of the character by the empty string. We can use the Replace method of the String class, but this can also be done using successively Split or Join. However, for a single character Replace is preferable.

Example:

static void Main(string[] args)
        {
            string s = "AA123456789AAABCDEF";
            Console.WriteLine(s.Replace("A",string.Empty));
            Console.ReadKey();
        }

Result:

123456789BCDEF

Deleting several characters from a string

If you have several characters to delete from a string, then you can use Split which accepts a character array, followed by Join to convert to a string.

Example:

static void Main(string[] args)
        {
            string s = "AA123456789AAABCDEF";
            Console.WriteLine(String.Join(string.Empty, s.Split(new char[] {'2', '6'})));
            Console.ReadKey();
        }

Result:

AA1345789AAABCDEF

Removing leading and trailing characters from a string

Deleting a substring from a string

Deleting characters matching a pattern

Exercises

We offer you some application exercises.

Exercise:

Destroy odd character of a chain starting from the first.

Solution:

 static void Main(string[] args)
        {
            string s = "123456789ABCDEF";
            int len = s.Length;
            int n = (len + 1)/ 2;
            char[] a = new char[n];
            int j = 0;
            for (int i = 0; i < len; i+=2)
            {
                a[j] = s[i];
                j++;
            }
            Console.WriteLine(new string(a));
            Console.ReadKey();
        }

Exercise:

Delete all groups of two adjacent numeric characters from the beginning but not isolated digits.

Solution:


CSharp

Categories

Share

Follow


KodFor Privacy Policy