Problem
Let’s pretend we have the following string.
string data= "/temp string";
We can remove the first character / in a variety of methods, including:
data.Remove(0,1);
data.TrimStart('/');
data.Substring(1);
But I’m not sure which one has the greatest algorithm and can do it faster. Is there a finest one, or are they all the same?
Asked by Amr Badawy
Solution #1
The second option differs from the others in that if the string is “/foo,” it will become “foo” rather than “/foo.”
The first choice takes a little more effort to grasp than the third; I consider the Substring option to be the most common and readable.
(Obviously, each one by itself won’t do anything; you’ll need to assign the result to a variable, perhaps data.)
I wouldn’t consider performance unless it was becoming a problem for you; in that case, the only way to tell would be to create test cases, and it’s then simple to run those test cases for each choice and compare the results. I expect Substring to be the fastest in this case, simply because Substring always creates a string from a single chunk of the original input, whereas Remove must at the very least glue together a start and an end chunk.
Answered by Jon Skeet
Solution #2
I realize we’re in hyper-optimization territory, but it seemed like a good reason to rev up BenchmarkDotNet. The conclusion of this test (on.NET Core, no less) is that Substring is marginally faster than Remove, with 19.37ns vs. 22.52ns for Remove in this example. As a result, some are 16 percent faster.
using System;
using BenchmarkDotNet.Attributes;
namespace BenchmarkFun
{
public class StringSubstringVsRemove
{
public readonly string SampleString = " My name is Daffy Duck.";
[Benchmark]
public string StringSubstring() => SampleString.Substring(1);
[Benchmark]
public string StringRemove() => SampleString.Remove(0, 1);
public void AssertTestIsValid()
{
string subsRes = StringSubstring();
string remvRes = StringRemove();
if (subsRes == null
|| subsRes.Length != SampleString.Length - 1
|| subsRes != remvRes) {
throw new Exception("INVALID TEST!");
}
}
}
class Program
{
static void Main()
{
// let's make sure test results are really equal / valid
new StringSubstringVsRemove().AssertTestIsValid();
var summary = BenchmarkRunner.Run<StringSubstringVsRemove>();
}
}
}
Results:
BenchmarkDotNet=v0.11.4, OS=Windows 10.0.17763.253 (1809/October2018Update/Redstone5)
Intel Core i7-6700HQ CPU 2.60GHz (Skylake), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=3.0.100-preview-010184
[Host] : .NET Core 3.0.0-preview-27324-5 (CoreCLR 4.6.27322.0, CoreFX 4.7.19.7311), 64bit RyuJIT
DefaultJob : .NET Core 3.0.0-preview-27324-5 (CoreCLR 4.6.27322.0, CoreFX 4.7.19.7311), 64bit RyuJIT
| Method | Mean | Error | StdDev |
|---------------- |---------:|----------:|----------:|
| StringSubstring | 19.37 ns | 0.3940 ns | 0.3493 ns |
| StringRemove | 22.52 ns | 0.4062 ns | 0.3601 ns |
Answered by Nicholas Petersen
Solution #3
Remove and Substring are likely to tie for first place since they both slurp up a fixed-size chunk of the string, but TrimStart scans the string from the left with a test on each character and then needs to do the same work as the other two techniques. But, seriously, this is splitting hairs.
Answered by Marcelo Cantos
Solution #4
If you were truly concerned, you could create a profile for it. See what happens if you write a loop with several iterations. However, this is unlikely to be the bottleneck in your program, and TrimStart appears to be the most semantically accurate option. Prior to optimizing, make an effort to write readable code.
Answered by Stefan Kendall
Solution #5
This also works in.Net Core:
data = data[1..];
Answered by Vasilis Plavos
Post is based on https://stackoverflow.com/questions/3222125/fastest-way-to-remove-first-char-in-a-string