It is not entirely clear why some lines are repeated several times. Explain, please.

public static void main(String[] args) { String s = "Мама мыла раму"; for (int i = 0; i < 10; i++) { System.out.println(s.substring(i).trim()); } } 

And the output will be:

  • Mom washed the frame

  • ama soap frame

  • ma soap frame

  • and washed the frame

  • soap frame

  • soap frame

  • ...

    2 answers 2

    When iterating at the beginning, you get a space, then the letter "M", but both lines look the same because .trim () cuts spaces around the edges.

    • Thanks, really did not take into account the spaces - Alex Sh.

    Perhaps a short example will be clear

     "Мама мыла раму".trim().equals(" Мама мыла раму".trim()); // true 

    This is because the trim () method removes spaces at the beginning and end of the line. And the line " Мама мыла раму".trim() converted to "Мама мыла раму"