Java Stack Usage removeParenthesis(String text)

Here you can find the source of removeParenthesis(String text)

Description

Removes parenthesis in a string

License

Open Source License

Parameter

Parameter Description
Text original string

Return

altered string

Declaration

public static String removeParenthesis(String text) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.Stack;

public class Main {
    /**//  ww  w .  j  a  v  a 2 s.  c  o m
     * Removes parenthesis in a string
     * @param Text original string
     * @return altered string 
     */
    public static String removeParenthesis(String text) {
        int pos = text.indexOf('(');

        if (pos == -1)
            return text;

        Stack<Integer> stack = new Stack<Integer>();

        pos = 0;

        while (pos < text.length()) {
            if (text.charAt(pos) == '(') {
                stack.push(pos);
            } else if (text.charAt(pos) == ')') {
                if (stack.isEmpty())
                    return text;

                Integer start = stack.pop();

                text = text.substring(0, start) + text.substring(pos + 1);

                pos = start;

                continue;
            }

            pos++;
        }

        return text.replace("  ", " ").trim();
    }

    /**
     * Checks if an ArrayLis is null or empty
     * @param AL the list
     * @return true/false
     */
    public static boolean isEmpty(ArrayList AL) {
        if (AL == null) {
            return true;
        }
        if (AL.size() == 0) {
            return true;
        }

        return false;
    }
}

Related

  1. getSyllables(String pinyin)
  2. isDoubleQuote(Stack bufStack)
  3. normalizeAbsolutePath(String curDir)
  4. removeBackets(String cont)
  5. removeDotSegments(String relativePath)
  6. resolveOneLineExpression(String line, String space, List target)
  7. resolveSeparateIfSentence(List lines, int cursor, List target, String space)
  8. split(final String str)
  9. split(String string, String token)