Fastest way to remove first char in a String

前端 未结 4 725
南方客
南方客 2020-12-07 13:56

Say we have the following string

string data= \"/temp string\";

If we want to remove the first character / we can do by a lot

4条回答
  •  天命终不由人
    2020-12-07 14:16

    The second option really isn't the same as the others - if the string is "///foo" it will become "foo" instead of "//foo".

    The first option needs a bit more work to understand than the third - I would view the Substring option as the most common and readable.

    (Obviously each of them as an individual statement won't do anything useful - you'll need to assign the result to a variable, possibly data itself.)

    I wouldn't take performance into consideration here unless it was actually becoming a problem for you - in which case the only way you'd know would be to have test cases, and then it's easy to just run those test cases for each option and compare the results. I'd expect Substring to probably be the fastest here, simply because Substring always ends up creating a string from a single chunk of the original input, whereas Remove has to at least potentially glue together a start chunk and an end chunk.

提交回复
热议问题