2012-05-03

Java String print tip

Are you getting confused with all the substring() indices during a debug session?
Try the following code:
System.err.println(myStr);
for (int i=0; i < myStr.length(); i++)
    System.err.print(i % 10);

You will get the following output:
my string value
012345678901234

% performs a division remainder operation, giving, in this case, always a number between 0 and 9.
And the numbers in the next line make it so much easier to see the string indices! :)

No comments: