How to remove N characters from the middle of a string in C#

1 Answer

0 votes
using System;

public class RemoveNCharactersFromMiddleOfString_CSharp {
    public static void Main(string[] args) {
        string str = "abc123def";
        
        int mid = str.Length / 2;
        int N = 3;
        
        str = str.Substring(0, mid - N / 2) + str.Substring(mid + 1 + N / 2);
        
        Console.WriteLine(str);
    }
}

 
 
/*
run: 
      
abcdef
      
*/

 



answered Sep 11, 2024 by avibootz

Related questions

1 answer 222 views
1 answer 204 views
1 answer 228 views
1 answer 233 views
...