How can I split string fastly in Java? [closed]

About the fastest you can do this is to use String.indexOf:

int pos = mymessage.indexOf('@');
String[] mysplit = {mymessage.substring(0, pos), mymessage.substring(pos+1)};

But I doubt it will be appreciably faster than:

String[] mysplit = mymessage.split("@", 2);

I suspect it might be slightly faster to use indexOf, because you’re not having to use the full regex machinery. But I think the difference would be marginal.

Leave a Comment