Java – find in string and start from the right

Maybe I am making this too complicated but I think I need to start reading a string from the right to find its position. Here is my sample:


In this string I need to find all the ‘1’ and get the position that they are on. In this case it would be 1,3 and 19.

Is that an easy way to do this?

Thank you all.


    String mystring = "10000000000000000101";
    for(int i=0; i < mystring.length(); i++){
        if(mystring.charAt(i) == '1'){
             int rightPosition = mystring.length() - i; 
             // do what ever you want with character and its position

Leave a Reply

Your email address will not be published. Required fields are marked *