Use the right tool for the job rather than focusing on micro-optimizations with manual string meddling - the only correct answer IMO.
– GimbyDec 3 '14 at 9:09

Yeah, you are quite right , but anyway it depends on the tasks....
– MaksymDec 3 '14 at 9:19

1

But the difference is quite huge, indexof+substring 55 times faster , but not so safe. So if you need performance you need to use indexof+substring if not then you should more care about safety.
– MaksymDec 3 '14 at 9:22

yeah, I did it exactly the same way, do you think it's the best way to do it?
– MaksymDec 3 '14 at 8:51

mine is better, if "?" is not there you get an index out of rage
– PbxManDec 3 '14 at 8:52

There is always a tradeoff between space and time. I think with split you will end up creating two different object if you encounter "?". Also split treats split pattern as regex which is not needed in your case and hence i think split would be overhead.
– SMADec 3 '14 at 8:59

I wrote test and seems like I have the most faster one :)
– MaksymDec 3 '14 at 9:08

I would agree with the same. Just see i updated the answer to be more robust code.
– SMADec 3 '14 at 9:10

So the most faster way is str.substring(0, str.indexOf('?')). Thanks everyone.
– MaksymDec 3 '14 at 11:11

Why is the second one almost twice as slow as the third? What extra work is it doing?
– JayenMay 19 '17 at 6:44

1

Why is this the accepted answer? OP never indicated the goal is performance, nor that he is doing millions of such operations in a tight loop. Finally, this is not even a correct way to benchmark - IANAE, but there is no pre-warming, and JIT is gonna distort the results all over the place. See JMH for proper benchmarking.
– vitalySep 3 '17 at 5:30