String str = "{1}";现在要把{}去掉,只留1,呵呵,代码怎么写

解决方案 »

  1.   


      String str = "{1}"; 
      System.out.println(str.replaceAll("[^0-9]", ""));
      

  2.   

            String str = "{1}";
            
            System.out.println(str.replaceAll("[{}]",""));
      

  3.   

    System.out.println(str.replaceAll("[{}]",""));
      

  4.   

    做个状态机:
    [code]
    public class Main {
    @Test
    public void testStrip(){
    String s = "{10}";
    String ss = strip(s);

    Assert.assertEquals("10", ss);

    }

    String strip(String s){
    StringBuilder b = new StringBuilder();
    final int P1 = 0x1;
    final int P2 = 0x2;
    final int CHAR = 0x4;
    int expecting = P1;
    int state = 0;
    for(int i = 0; i < s.length(); i++ ){
    char c = s.charAt(i);
    if(c == '{' && (expecting & P1) > 0){
    state = 1;
    expecting = CHAR;
    }else if( c== '}' && (expecting & P2) > 0){
    break;
    }else if((expecting & CHAR) > 0 && state == 1){
    b.append(c);
    expecting = P2 | CHAR;
    }
    }
    return b.toString();
    }
    }
    [code]
      

  5.   

    public class TestSplit {
    public static void main(String[] args) {
    /**
     * 思路:用正则
     */
    String s1 = "{1}";
    System.out.println(s1.replaceAll("[^0-9]", ""));
    }
    }