How to check if the end of the String is one or zero?

13

How can I check if at the end of a string it is 0 ?

The problem with this code is that string can contain:

  

jrp_documento.jrp_doc_001provider | 0

I need to check if the end of the string is 1 or 0 .

I have identified another problem as well. Since this string is the name of the table in the database it is necessary to be the same as the table, I give replaceAll there in 0 if it contains 0 , but if string contains% as_% it exits and is not to be removed unless 0 is at the end. What do I do?

 public static void main(String[] args) {

    String nomeColuna = null;
    String coluna = null;
    String sampleString = "jrp_documento.jrp_doc_fornecedor|0";

    String[] items = sampleString.split(";");
    List<String> itemList = Arrays.asList(items);

    for(int i = 0; i < itemList.size(); i++){
       coluna = items[i].replaceAll("[|]", "");            
       coluna =  coluna.replaceAll("[.]", "_");
       if(coluna.contains("0")){

           nomeColuna = coluna.replaceAll("0", "");
           System.out.println(nomeColuna);
           System.out.println(" é zero ");

      }else{

        nomeColuna = coluna.replaceAll("1", "");
        System.out.println(nomeColuna);
        System.out.println(" é um ");

      }             
  }
    
asked by anonymous 08.08.2016 / 16:31

3 answers

12

You can use the String#endsWith :

System.out.println("jrp_jasper.jrp_jas_nome|0".endsWith("0"));      // true
System.out.println("jrp_jasper.jrp_jas_sobrenome|1".endsWith("0")); // false

You can also use String#substring :

String sampleString1 = "jrp_jasper.jrp_jas_nome|0";
String sampleString2 = "jrp_jasper.jrp_jas_sobrenome|1";

String ultimo1 = sampleString1.substring(sampleString1.length() - 1);
String ultimo2 = sampleString2.substring(sampleString2.length() - 1);

System.out.println(ultimo1.equals("0")); // true
System.out.println(ultimo2.equals("0")); // false

Editing

  

I have identified another problem as well. Since this string is the name of the   table in the database it is necessary to stay the same as the table, I give    replaceAll there is 0 if it contains 0, but if the string contains 0 it removes and   it is not to withdraw unless a 0 is at the end. What do I do?

String#replaceAll , the String#replace also works in this case.

Choose one of the above methods to check the end of the string , if the condition you want is satisfied, extract all the characters except the last one, here's a way to do this with String#substring :

String sampleString = "jrp_jasper.jrp_jas_nome|0;jrp_jasper.jrp_jas_sobrenome|1";
String[] items = sampleString.split(";");

for(String item: items){
    String nomeColuna = item.replace("|", "").replace(".", "_");

    if (nomeColuna.endsWith("0")){
        // Se terminar em 0
        nomeColuna = nomeColuna.substring(0, nomeColuna.length() - 1);
        System.out.println(nomeColuna);
    }
    else if (nomeColuna.endsWith("1")){
        // Se terminar em 1
        System.out.println(nomeColuna);
    }
    else{
        // Não termina em 0 nem 1
        System.out.println(nomeColuna);
    }
}

See demonstração

    
08.08.2016 / 16:49
13
    String sampleString = "jrp_documento.jrp_doc_fornecedor|0";
    String[] items = sampleString.split(";");
    for(int i = 0; i < items.length; i++){
        if(items[i].charAt(items[i].length()-1) == '0'){
            System.out.println(items[i] + " termina em zero");
        }else{
            System.out.println(items[i] + " nao termina em zero");
        }   
    }

The above code takes String , isolates the last character with charAt , and check if it equals 0.

See the IDEONE .

    
08.08.2016 / 16:44
12

To check at the end use endsWith, eg:

str.endsWith("0") || str.endsWith("1")
    
08.08.2016 / 16:48