How can I get inside parentheses value in a string?
String str= "United Arab Emirates Dirham (AED)";
I need only AED text.
The below method can split any String inside any bracket like '(. {. [, ), }, ]'.
public String getStringInsideChars(String original, char c){
this.original=original;
original = cleaning(original);
for(int i = 0; i < original.length(); i++){
if(original.charAt(i) == c){
String temp = original.substring(i + 1, original.length());
i = original.length();//end for
for(int k = temp.length() - 1; k >= 0; k--){
if(temp.charAt(k) == getReverseBracket(c)){
original = temp.substring(0, k);
k = -1; // end for
}
}
}
}
return original;
}
private char getReverseBracket(char c){
return c == '(' ? ')' :
c == '{' ? '}' :
c == '[' ? ']' :
c == ')' ? '(' :
c == '}' ? '{' :
c == ']' ? '[' : c;
}
public String cleaning(String original) {
this.original = original;
return original.replaceAll(String.valueOf('\t'), "").replaceAll(System.getProperty("line.separator"), "");
}
You can use it like below :
getStringInsideChars("{here is your string}", '{')
it will return "here is your string"
i can't get idea how to split inside parentheses. Would you help highly appreciated
When you split you are using a reg-ex, therefore some chars are forbidden.
I think what you are looking for is
str = str.split("[\\(\\)]")[1];
This would split by parenthesis. It translates into split by (
or )
. you use the double \\
to escape the paranthese which is a reserved character for regular expressions.
If you wanted to split by a .
you would have to use split("\\.")
to escape the dot as well.
You could try:
String str = "United Arab Emirates Dirham (AED)";
int firstBracket = str.indexOf('(');
String contentOfBrackets = str.substring(firstBracket + 1, str.indexOf(')', firstBracket));
Compiles and prints "AED". Even works for multiple parenthesis:
import java.util.regex.*;
public class Main
{
public static void main (String[] args)
{
String example = "United Arab Emirates Dirham (AED)";
Matcher m = Pattern.compile("\\(([^)]+)\\)").matcher(example);
while(m.find()) {
System.out.println(m.group(1));
}
}
}
The regex means:
\\(
: character (
(
: start match group[
: one of these characters^
: not the following character)
: with the previous ^
, this means "every character except )
"+
: one of more of the stuff from the []
set)
: stop match group\\)
: literal closing paranthesisyes, you can try different techniques like using
string.indexOf("(");
to get the index and the use
string.substring(from, to)
The answer from @Cœur is probably correct, but unfortunately it did not work for me. I had text similar to this:
mrewrwegsg {text in between braces} njanfjaenfjie a {text in between braces}
It was way larger, but let's consider only this short part.
I used this code to get each text between bracelet and print it to the console:
Pattern p = Pattern.compile("\\{.*?\\}");
Matcher m = p.matcher(test);
while(m.find()) {
System.out.println(m.group().subSequence(1, m.group().length()-1));
}
It might be more complicated than @Cœur answer, but maybe there is someone like me, who can find my answer useful.