On Tue, 20 May 2008 10:30:48 -0700, Man Alive <nopsam@example.com>
wrote, quoted or indirectly quoted someone who said :
int commaLocation = stringName.indexOf(",");
int stringLengthBeforeComma = (stringName.length() - (commaLocation
+1));
System.out.Println("Part of String before comma: "
+stringName.substring(0,stringLengthBeforeComma));
I would code that as:
final int commaPlace = s.indexOf( ',' );
if ( commaPlace < 0 )
{
throw new IllegalArgumentException( "missing comma");
}
final String state = s.substring( 0, commaPlace );
final String city = s.substring( commaPlace+1 );
other ways, especially if you had more fields.
1. use a regex spit. See http://mindprod.com/jgloss/regex.html
2. use CSVReader See http://mindprod.com/jgloss/csv.html
regex split is actually slower than a hand-coded indexOf based parser.
On the other hand, it is easier to get correct. If this is something
regex. If it's something that happens once when a user clicks go, use