/**
* Remove all parentheses and the text inside them.
*
* @param text The input string
* @return The cleaned string
*/
public class Main {
public static String removeParenthesesWithContent(String text) {
// Remove parentheses and everything inside them
String cleaned = text.replaceAll("\\([^)]*\\)", " ");
// Collapse multiple spaces into one (idiomatic Java)
String collapsed = String.join(" ", cleaned.trim().split("\\s+"));
// Final trim of leading/trailing spaces
return collapsed.trim();
}
public static void main(String[] args) {
String str =
"(An) API (API) (is a) (connection) connects (between) computer programs";
String output = removeParenthesesWithContent(str);
System.out.println(output);
}
}
/*
run:
API connects computer programs
*/