Search code examples
javaresultset

Split resultset with '|' and get the second value for each row


I have a Db column where I need to split the resultset by '|'. I need to get the second value for each row that is populating

String example 
First row 34567|001
Second row 34545|002

I want to get 001 and 002 and I want to check whether I am getting 001 and 002.

String[] res = TBOOKER_TRD_INTFTable.getString("EXT_T_ID_C").split("|");
System.out.println(res);
String [] version=res.replaceAll("\\s", "").split("|",0);
List ves=Arrays.asList(version);
System.out.println(ves);
if(ves.contains("001") && (ves.contains("002")){
  System.out.println("version is correct");
}

Solution

  • Just split the column value on pipe and then check the second value:

    boolean correct = false;
    String[] parts = TBOOKER_TRD_INTFTable.getString("EXT_T_ID_C").split("\\|");
    if (parts.length > 1) {
        if ("001".equals(parts[1]) || "002".equals(parts[1])) {
            correct = true;
        }
    }