How can I get inside parentheses value in a string?

前端 未结 10 724
半阙折子戏
半阙折子戏 2020-12-23 13:35

How can I get inside parentheses value in a string?

String str= "United Arab Emirates Dirham (AED)";

I need only AED text.

相关标签:
10条回答
  • 2020-12-23 14:11

    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"

    0 讨论(0)
  • 2020-12-23 14:12

    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.

    0 讨论(0)
  • 2020-12-23 14:16

    You could try:

    String str = "United Arab Emirates Dirham (AED)";
    int firstBracket = str.indexOf('(');
    String contentOfBrackets = str.substring(firstBracket + 1, str.indexOf(')', firstBracket));
    
    0 讨论(0)
  • 2020-12-23 14:17

    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 paranthesis
    0 讨论(0)
  • 2020-12-23 14:20

    yes, you can try different techniques like using

     string.indexOf("("); 
    

    to get the index and the use

    string.substring(from, to)
    
    0 讨论(0)
  • 2020-12-23 14:20

    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.

    0 讨论(0)
提交回复
热议问题