Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,023 questions

51,970 answers

573 users

How to remove the middle word from a string in C#

2 Answers

0 votes
using System;
using System.Linq;

public class RemoveMiddleWordFromString
{
    public static void Main()
    {
        string str = "c# c c++ java rust";

        string result = RemoveMiddleWord(str);

        Console.WriteLine(result);
    }

    public static string RemoveMiddleWord(string input) {
        // Split the string into words
        string[] words = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);

        // If there are fewer than 3 words, nothing to remove
        if (words.Length <= 2)
            return input;

        // Calculate the middle index
        int midIndex = words.Length / 2;

        // Create a new string without the middle word
        return string.Join(" ", 
            words.Take(midIndex).Concat(words.Skip(midIndex + 1)));
    }
}


/*
run:

c# c java rust

*/

 



answered Jan 20, 2017 by avibootz
edited Dec 24, 2025 by avibootz
0 votes
using System;
using System.Linq;

class Program
{
    static string RemoveMiddleWord(string s) {
        var words = s.Split(' ', StringSplitOptions.RemoveEmptyEntries);
        
        if (words.Length <= 2) return s;

        int mid = words.Length / 2;
        
        return string.Join(" ", words.Where((w, i) => i != mid));
    }

    static void Main()
    {
        string s = "c c++ rust java python";

        Console.WriteLine(RemoveMiddleWord(s));
    }
}



/*
run:

c c++ java python

*/

 



answered Dec 3, 2024 by avibootz
edited Dec 24, 2025 by avibootz
...