How to find a character in a String and print the character position along the entire string? For example, I want to find the position of 'o' in this line: "you are awesome honey" and get the answer = 1 12 17 .
I wrote this, but it does not work:
public class Pos { public static void main(String args[]){ String string = ("You are awesome honey"); for (int i = 0 ; i<string.length() ; i++) if (string.charAt(i) == 'o') System.out.println(string.indexOf(i)); } }
java string character
Rastin radvar
source share