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 Java

1 Answer

0 votes
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class RemoveMiddleWord {
    public static void main(String[] args) {
        String str = "c# c c++ java rust";

        String result = removeMiddleWord(str);

        System.out.println(result);  // c# c java rust
    }

    public static String removeMiddleWord(String input) {
        String[] words = input.trim().split("\\s+");

        if (words.length <= 2) {
            return input; // nothing to remove
        }

        int mid = words.length / 2;

        return IntStream.range(0, words.length)
                .filter(i -> i != mid)
                .mapToObj(i -> words[i])
                .collect(Collectors.joining(" "));
    }
}


 
/*
run:
 
c# c java rust
 
*/

 



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