Java Online Compiler
Example: Remove Brackets using multiple replace calls in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Remove Brackets using multiple replace calls import java.util.Scanner; // Main class containing the entry point of the program public class Main { public static void main(String[] args) { // Step 1: Define the algebraic expression String expressionWithBrackets = "{A + (B * [C - D]) / E}"; System.out.println("Original Expression: " + expressionWithBrackets); // Step 2: Apply String.replace() for each type of bracket String tempExpression = expressionWithBrackets.replace("(", ""); // Remove opening parentheses tempExpression = tempExpression.replace(")", ""); // Remove closing parentheses tempExpression = tempExpression.replace("[", ""); // Remove opening square brackets tempExpression = tempExpression.replace("]", ""); // Remove closing square brackets tempExpression = tempExpression.replace("{", ""); // Remove opening curly braces String expressionWithoutBrackets = tempExpression.replace("}", ""); // Remove closing curly braces // Step 3: Print the modified expression System.out.println("Expression without Brackets (Multi-replace): " + expressionWithoutBrackets); // Example with another expression String anotherExpression = "([val1 + val2] * {val3 - val4})"; System.out.println("\nOriginal Expression: " + anotherExpression); String result = anotherExpression.replace("(", "").replace(")", "") .replace("[", "").replace("]", "") .replace("{", "").replace("}", ""); System.out.println("Expression without Brackets (Multi-replace): " + result); } }
Output
Clear
ADVERTISEMENTS