最快的方法,以消除第一个字符的字符串 [英] Fastest way to remove first char in a string

查看:166
本文介绍了最快的方法,以消除第一个字符的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有以下字符串

string data= "/temp string";

如果我们要删除的第一个字符 / 我们可以通过方法如很多做的:

If we want to remove the first character / we can do by alot of ways such as :

data.Remove(0,1);
data.TrimStart('/');
data.Substring(1);



但真的..我不知道哪一个有最好的算法,做更快..

是否有一个是最好的或者都是一样的吗?

推荐答案

第二个选择真的是不一样的人 - 如果字符串为///富就会变成富,而不是//富

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.

这篇关于最快的方法,以消除第一个字符的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆