String.Remove vs String.Substring

  • Thread starter Thread starter Janaka
  • Start date Start date
J

Janaka

Does anyone know if there's any performance difference between using these
two methods or in what circumstances you should use one over the other? For
instance if I want to cut off the first 10 characters in my string I could
use either
myString.Remove(0,10);
myString.Substring(11);

Thanks,

Janaka
 
Does anyone know if there's any performance difference between using these
two methods or in what circumstances you should use one over the other? For
instance if I want to cut off the first 10 characters in my string I could
use either
myString.Remove(0,10);
myString.Substring(11);

It's quite easy to check this. According to some benchmarks I've
performed Remove is about 40-50% slower than Substring.

regards
Bogdan
 
Substring should be faster, as the method is basically saying, make a new
string starting at this point. With remove, you are asking it to evaluate
the first 10 characters and remove them to create the new string. Whether
the performance difference makes a difference is another story. It is
likely, unless you app is being hammered, that either method will make a
major difference in terms of perceived performance. As neither is easier to
maintain, I would opt for performance and use SubString instead.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

**********************************************************************
Think Outside the Box!
**********************************************************************
 
Back
Top