Take the 2-minute tour ×

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.

share|improve this question
Your string length is 20 by the way. So it would be 1,3 and 20. –  Kal Aug 7 '11 at 16:13
In some UNIX utilities, positions from the right are represent as negative number stating at -1. This makes it easy to difference zero indexed from the left. You might want to apply this convention to your code. –  jfgagne Aug 7 '11 at 16:13

7 Answers 7

up vote 2 down vote accepted
    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
share|improve this answer
Thank you everybody for all your code in such a short time. I used the last one, that seems to work really well but I am sure all the other code is great also. So how does this work now? Do check them all? –  Steve Aug 7 '11 at 16:47
Accept the solutions that solve your problem. :) –  mjisrawi Aug 7 '11 at 17:31

Given that you need to find all the positions, I don't think you really need to find the positions from right to left - you can find their "normal" indexes, and just compute how many positions that would be from the right.

So use indexOf(int, int) repeatedly, and subtract the returned index from the length of your string:

public static void showSetBits(String text)
    int lastPosition = -1;
    while ((lastPosition = text.indexOf('1', lastPosition + 1)) != -1)
        System.out.println("Found set bit at position " + 
                           (text.length() - lastPosition));

If you ever do want to go from right to left, you can always use lastIndexOf.

share|improve this answer

Use java.lang.String#indexOf to find the index from the left.

Then subtract from the length of the string.

    String abc = "10000000000000000101";
    int fromIndex = 0;
    int idx = 0;
    do {
        idx = abc.indexOf("1", fromIndex);
        if (idx == -1)
        System.out.println(abc.length() - idx);
        fromIndex = idx + 1;
    } while (idx != -1);
share|improve this answer

One way would be to use the reverse method in the StringBuilder class. So first we reverse the string:

//declare and initialise string
String str = "10000000000000000101";

//First we take an instance of the StringBuilder, then
//we append the original string to it and lastly we reverse it and get the string value.
String reversed = new StringBuilder().append(str).reverse().toString();

Now we have reversed the string all you just have to do is loop through the string and keep track of the index at which you encounter a 1. You can easily use an ArrayList. Each time you encounter a 1, you add the index to that list.

So lets declare an ArrayList of generic type Integer first and then loop through the reversed string.


//we loop through the string..

for(int i=0; i<reversed.length(); i++)
    if(reversed.charAt(i) == '1'){

Now you have the list of indices so you can just print them out or do whatever you want with them. For a problem like yours, there are a few shorter ways to do this but I explained a more detailed and efficient way you could use to tackle such a problem if you do not have any idea what the original string looks like(say you just have a text file with millions of lines or whatever)

share|improve this answer
The original question needed one indexed position, and this solution gives zero indexed position. –  jfgagne Aug 7 '11 at 20:31
Even if this solution shows a nice usage of the reverse method of StringBuilder, I believe allocating memory to solve this problem is a waste of resource. –  jfgagne Aug 7 '11 at 20:33

@mjisrawi Your code has some errors do check.

@steve ": String.charAt(char) is probably the easiest way to find out the number of instances, when used inside some control statement. Ofcourse you can get it done using other pre configured methods like indexOf()

 String s = "100000000001000000101";

 int len = s.length();
 int count =0;

 while (len>0) {     
      if (s.charAt(len-1)=='1'){
          count ++;}                                     
     len --;
     System.out.println("Number of 1 using String.charAt()" + " = " +count);
share|improve this answer

Probably the least verbose I can think of uses String split:

String str = "10000000000000000101";
String[] bits = str.split("1");
System.out.println("Number of 1s: " + (bits.length-1));

Some people may consider this a bit of a hack or a bit inefficient, but it works fine for simple scenarios.

share|improve this answer
String searchString = "10000000000000000101";
for (int i = searchString.length; i >= 0; i--) {
    if (searchString.charAt(i).equals('1')) {
        System.out.println("" + i);

This should do it

share|improve this answer
This will evaluate from the left to right. He wants right to left. –  Kal Aug 7 '11 at 15:54
Oops! I'll change it. –  fireshadow52 Aug 7 '11 at 15:55
Even if this solution shows a nice usage of the toCharArray method of String, I believe allocating memory to solve this problem is a waste of resource. –  jfgagne Aug 7 '11 at 20:36

Your Answer


