Which method is better

A

Andrea

Which method is better to remove a char from a string?

A replace or a substring?

Considering that first char is at 0 position. Using reflector, replace let
lost its track in internal call, while substring show what it really does,
so I'm not able to evaluate the efficiency.

Any suggestion?

Thanks
Andrea
 
D

Dustin Campbell

Which method is better to remove a char from a string?
A replace or a substring?

Considering that first char is at 0 position. Using reflector, replace
let lost its track in internal call, while substring show what it
really does, so I'm not able to evaluate the efficiency.

Any suggestion?

When you see an [MethodImpl(MethodImplOptions.InternalCall] attribute on
a .NET framework method in the Reflector, that means that the method is implemented
inside the runtime itself -- usually to make the method faster or access
some internal data structure. If you want to see how these methods are likely
implemented, check out the Shared Source CLI from http://msdn.microsoft.com/net/sscli.

String.Replace(Char, Char) : String is implemented at sscli\clr\src\vm\comstring.cpp\COMString::Replace
String.Replace(String, String) : String is implemented at sscli\clr\src\vm\comstring.cpp\COMString::ReplaceString

Looking at this source, while interesting, won't really give you a sense
of efficiency. First of all, it's not necessarily how the .NET framework
commercial distribution is implemented under-the-hood. And secondly, comparing
un-JITted managed code against native code is pretty futile.

A good way to test efficiency is to create a small test bench that executes
each method several times and compares the timings. If you are using .NET
2.0, you can use the System.Diagnostics.Stopwatch to time the methods with
fair accuracy. Be sure to take several samples and average the timings so
that you can get more accurate readings as other processes running on your
system and the garbage collector may intrude on your benchmarking.

Best Regards,
Dustin Campbell
Developer Express Inc.
 
J

Jon Skeet [C# MVP]

Andrea said:
Which method is better to remove a char from a string?

A replace or a substring?

Replace won't remove it - it will replace it. So just from a
correctness point of view, you want Substring.
 
B

Brian Gideon

Andrea,

It depends on how you want to remove a character. If you want to
remove one or more occurrences of a specific character then Replace
would be your best bet. If you want to remove a character at a
specific position then Substring might be a better option.

Brian
 
S

Stephany Young

And the Remove mthod will be evne better than either the Replace or
SubString methods.
 
B

Bill Butler

Jon Skeet said:
Replace won't remove it - it will replace it. So just from a
correctness point of view, you want Substring.

Replace can work just fine to remove characters
I don't know which way is more efficient, but it does work.

string before ="Hello";
string after = before.Replace("H","");

Bill
 
J

Jon Skeet [C# MVP]

Bill Butler said:
Replace can work just fine to remove characters
I don't know which way is more efficient, but it does work.

string before ="Hello";
string after = before.Replace("H","");

Apologies - I was thinking of Replace(char, char) for some reason. Not
sure why!

I'd use Substring if I wanted to remove a character from a specific
position, and Replace if I wanted to replace all instances of a
character.
 
A

Andrea

Well considering a replace with empty string, the result is always a remove.
In all case, for what I've seen, all the methos need to manipulate a string
and return a
new string, so, as Dustin suggested the best way is have some test on
specific case
and see the amout of time and cycle that framework need to execute a spcific
path.

Thanks to all.
Andrea
 
D

Dustin Campbell

Which method is better to remove a char from a string?
A replace or a substring?

Considering that first char is at 0 position. Using reflector, replace
let lost its track in internal call, while substring show what it
really does, so I'm not able to evaluate the efficiency.

Any suggestion?

I don't think anybody's mentioned this yet: if you are doing more than one
manipulation of your string (e.g. remove the first character and then some
other character), you will gain more efficiency by using a System.Text.StringBuilder
(which actually has a "Remove" method).

Best Regards,
Dustin Campbell
Developer Express Inc.
 
C

Christof Nordiek

Not, that the to method have different meanings.
In the case that the character to remove will always at the beginning and
never occur at any other position of your strings then Substring will be
faster because it doesn't need to search. But in any case both methods will
have to copy the resulting string into a new string-instance.

In any case, you chould consider what should happen, if the above condition
or whatever assumptions you make isn't true. And if the loss of readability
/ semantical corectness is worth the performance gain. Correct behaviour
and maintainability of an application is most often more worth than
performance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top