Question: [Someone on stackoverflow](https://stackoverflow.com/questions/12134872/how-do-i-find-out-the-position-of-a-char) gave this code that finds the position of chars... Now the problem is, its a loop that searches the whole string and returns each position... Now in this code an example "pool" is used. So it returns 1 and 2

```
String s = "Pool";
int idx = s.indexOf('o');
while (idx > -1) {
System.out.println(idx);
idx = s.indexOf('o', idx + 1);
}

```

Is there a way to use both positions returned in a method and replace the chars... For instance if I wanted to replace the 'o' with 'hh' resulting in 'phhl'

Answer: 
I would recommend the solution by @Sujay, but just in case that does not meet your requirement...

```
int counter = 0;
String input = "Pool";
char replacement = 'l';
char[] inputArray = input.toCharArray();
for(char ch : inputArray) {
    if(ch == 'o') {
        inputArray[counter] = replacement;
    }
    counter += 1;
}
String output = new String(inputArray);
System.out.println(output);

```
You might want to check the [`public String replaceAll(String regex, String replacement)`](http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#replaceAll%28java.lang.String,%20java.lang.String%29) method in `String`.

So for example, with what your code, you can do something like this:

```
String s = "pool";
System.out.println(s.replaceAll("o", "h"));

```