Search code examples
javajsonseleniumtestingui-testing

remove brackets from returned string


I currently call a json within my selenium tests and they are returned in the form of [4534535], id like to remove the brackets and leave just 4534535 but the code im currently using doesnt seem to do that and i cant find a solution.

Currently im using

String[] tmp = response_body_string.split("[\\[\\]]");
String val2 = tmp[0];
val3 = val2.replaceAll("[^0-9]", "");

The response_body_string is what ive received back from the json (please not sometimes theres is a list of code returned thats why im using string[] But it seems to be returning nothing, is the tmp[0] causing the issue?

Im tryign to use the code in another json and then pass the string of "4534535" on to the next one. Im able to use the code when theres more than one code number , for example [3343,221213] works fine because im using

String[] tmp = response_body_string.split(",");
tmp = tmp[1].split(",");
//Returns second game ID
String val2 = tmp[0];
val3 = val2.replaceAll("[^0-9]", "");

Solution

  • If I got you right, You want to remove only the brackets and get only the number. You can use:

    String[] tmp = response_body_string.split("[\\[\\]]");
    String tmp = tmp[0].substring(1,tmp.length-1);
    

    Hope I got you right.

    edit:

    If response_body_stringis a string(I assume yes Because you are using String function), you can just do the substring to it. like that:

    String Final = response_body_string.substring(1,response_body_string.length-1);