Question:
How do I find the index of all of the spaces in a string?
Answer:
You could convert the string to a char array and loop through it.
Code:
public class Spaces { public static void main(String[] args) { String str = "this is a string"; System.out.println("String: " + str); char[] array = str.toCharArray(); System.out.println("spaces at:"); for(int x=0; x<array.length; x++){ if(array[x] == ' '){ System.out.println(x); } } } }
Output:
$ java Spaces String: this is a string spaces at: 4 7 9