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

Asked

Viewed 1,093 times

12

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

The problem with this code is that the string may contain:

jrp_documento.jrp_doc_001fornecedor|0

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

I identified another problem as well. Like this string is the table name in the database it is necessary to stay equal to table, I give replaceAll there in the 0 hold back 0, but if the string contain 0 he withdraws and is not to withdraw unless the 0 is at the end. What 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 ");

      }             
  }
  • 1

    You need to check if items has 0 at the end?

  • Yes that’s right, and it cannot modify the other zeros that the String has, only remove the zero or one of the same end...

3 answers

12


You can use the function 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

It is also possible to use the function 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 identified another problem as well. Like this string is the name of table in database it is necessary to stay equal to table, I give replaceAll there in 0 if it contains 0, but if the string contains 0 it removes and not to be removed unless the 0 is at the end. What I do?

There is no need to 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 characters except the last one, below follows a way to do this with the 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

  • 1

    From what I understand, what needs to be checked are the Vector Strings items. I believe that samplestring of the code was for test purposes only, in another question, that string contained several other values separated by ;

  • @diegofm I edited the answer and put that too. :)

  • 1

    It worked out Oo. I’m studying the substring here to better see how it works. Thanks man, you saved my life. It’s always nice to see how other programmers program us learn a lot.

11

    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 code above takes the String, isolates the last character with charAt, and check that it is equal to 0.

See on IDEONE.

  • later I test.

  • Negative is easy, difficult to collaborate with the site and point out the error in the answer.

11

To check at the end use endsWith, ex:

str.endsWith("0") || str.endsWith("1")
  • That answer seems more accurate to me. But I still have to figure out a way to boot this 0 or 1 from the end only......

  • I’ll test later, I’ll watch class. I’ll talk later

  • simple brother, eg: if(str.endsWith("0") || str.endsWith("1")) {&#xA; str = str.substring(0,str.length() - 1);&#xA;} System.out.println(str);

  • 1

    sorry the formatting, I was doing something else and passed 5min and did not let edit :(

  • :) All right. Brother.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.