> Java pick third item in a list delimited by commas?

Java pick third item in a list delimited by commas?

Posted at: 2014-12-18 
Try this:

String last = str.split(", ")[2];

I have a string like this:

String str = "cat, dog, mouse, rat, sheep"

I need the third word out of that string, so just print mouse.

Right now, I have:

String last = str.substring(str.lastIndexOf(",") +2);

The above code snippet doesn't always return just the third word that is separated by commas.

Can someone help me? Thanks!