In Java, if you want to write:
substr($str, 0, $n)
You have to write something like:
str.substring(0, min(n, str.length - 1))
Because if your second index is greater than the length of the string, Java throws an exception, rather than realizing you wanted everything that far, or to the end of the string if you can't go that far.
Update 2008-02-22T19:37Z: This is apparently completely wrong. I realized that the next day, when I saw how one of my coworkers solved the same problem I was working on, and substring seemed to work for him. I have no idea why it didn't work for me. But I saw this today and thought, "Ew, I'd hate for anyone to read this and think it were actually true."