方法返回的字符串是这样的  XXXX[YYYY]
其中X,Y是动态的 不确定是多少字符 请问 怎么把[]中的Y内容取出来  不要[]! 
谢谢

解决方案 »

  1.   

    int start=string.indexof('[');这个方法可以找到[的下标
    然后从start这个下标开始到结尾的前一个坐标结束
    str=string.substring(start+1,string.length()-1);
      

  2.   


    public class Test {
    public static void main(String[] args) {
    String str = "xxx[yyy]";
    //"["所在位置
    int beginIndex = str.indexOf("[");
    //"]"所在位置
    int endIndex = str.indexOf("]");
    //如果"["和"]"存在
    if(beginIndex!=-1 && endIndex!=-1){
    String endString = str.substring(beginIndex+1, endIndex);
    System.out.println(endString);
    }
    }
    }
      

  3.   


    public static void main(String args[]) {
    String str="XXXX[YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY]";
    Pattern p=Pattern.compile("\\[(\\w+)\\]");
    Matcher m=p.matcher(str);
    while(m.find())
    System.out.println(m.group(1));
    }
      

  4.   

    二楼正解,还有就是用lastIndexof("[");lastIndexof("]"),也ok
      

  5.   

    public static void main(String[] args) {
    String s = "XXXX[YY啊啊啊啊#@#$%234234^&*((^%   YY]";
    System.out.println(s.replaceAll("(^.+\\[(.+?)\\]$)", "$2"));
    }
    输出:
    YY啊啊啊啊#@#$%234234^&*((^%   YY
      

  6.   

    多看看javaAPI...这些简单的问题就不要再发过来了...