I need to ask about the deletion of parenthesis and text within them in Java String. For example, I have
String str = "I am a new (Software) Engineer"
Now the question is, how to remove the substring "(software)" here without using
str.replace("(software)", "")
because may be in next string I would get "I am a new (Electrical) Engineer" or "(Mechanical") or something like that.
So how can I do this, I think one way is to get the index of "(" and ")" and removing/replacing them by using that indices but I hope there will be some shorter way to do this
Java allows you to easily replace a substring you want to delete from the String with an empty String. Syntax: string. replace(char oldChar, empty char)
You can use a regular expression and replaceAll() method of java. lang. String class to remove all special characters from String.
Use regex to match anything in brackets:
str = str.replaceAll("\\(.*?\\) ?", "");
Note that the brackets must be escaped, and that the escape character \
itself must be escaped in the String, hence the double backslashes.
The term .*?
is a reluctant match, which means that it won't skip all the way to the last bracket in case there are two bracketed terms in the input ie "a (foo) b (bar) c" would become "a c" if .*
was used, because it would consume everything from the first open bracket to the last close bracket.
I added an optional space at the end so you won't be left with two adjacent spaces once you remove the intervening term.
Actually, the most efficient and foolproof way would be to go through the entire String and check for brackets. It would require only a single sweep.
You would also have to keep track of the amount of parenthesis opened and closed, to make sure the algorithm is correct for sentences like outside (a text (in another) parenthesis) outside again
which should result in outside outside again
.
A code which does more or less that.
public class RemoveParenthesis {
public static void main(String[] args) {
int open = 0;
int closed = 0;
boolean changed = true;
int startIndex = 0, openIndex = -1, closeIndex = -1;
String text = "outside (a text (in another) parenthesis) outside again";
System.out.println("before: " + text);
while (changed) {
changed = false;
for (int a = startIndex; a < text.length(); a++) {
if (text.charAt(a) == '(') {
open++;
if (open == 1) {
openIndex = a;
}
} else if (text.charAt(a) == ')') {
closed++;
if (open == closed) {
closeIndex = a;
text = text.substring(0, openIndex)
+ text.substring(closeIndex + 1);
changed = true;
break;
}
} else {
if (open == 0)
startIndex++;
continue;
}
}
}
System.out.println("after: " + text);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With