The most elegant ways to iterate the words of a java string.


Just split the string based on space and then iterate it. See the example below −

Example

public class Tester {
   public static void main(String[] args) {
      String test = "I love learning Java";
      String[] subStrings = test.split(" ");
      for(String subString: subStrings) {
         System.out.println(subString);
      }
   }
}

Output

I
love
learning
Java

Updated on: 24-Feb-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements