Connecting Tech Pros Worldwide Forums | Help | Site Map

Which method is better

Andrea
Guest
 
Posts: n/a
#1: Jan 4 '07
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




Dustin Campbell
Guest
 
Posts: n/a
#2: Jan 4 '07

re: Which method is better


Which method is better to remove a char from a string?
Quote:
>
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::ReplaceS tring

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.


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#3: Jan 4 '07

re: Which method is better


Andrea <googlegroups@spam.no.cleanmail.itwrote:
Quote:
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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Brian Gideon
Guest
 
Posts: n/a
#4: Jan 4 '07

re: Which method is better


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

Andrea wrote:
Quote:
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
Stephany Young
Guest
 
Posts: n/a
#5: Jan 4 '07

re: Which method is better


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


"Andrea" <googlegroups@spam.no.cleanmail.itwrote in message
news:F4EEFD9E-4300-4BE6-8EE3-A337FB0373B1@microsoft.com...
Quote:
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

Bill Butler
Guest
 
Posts: n/a
#6: Jan 5 '07

re: Which method is better


"Jon Skeet [C# MVP]" <skeet@pobox.comwrote in message
news:MPG.2007855ff1a46acf98d737@msnews.microsoft.c om...
Quote:
Andrea <googlegroups@spam.no.cleanmail.itwrote:
Quote:
>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.
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
Quote:
>
--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#7: Jan 5 '07

re: Which method is better


Bill Butler <qwerty@asdf.comwrote:
Quote:
Quote:
Quote:
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.
>
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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Andrea
Guest
 
Posts: n/a
#8: Jan 5 '07

re: Which method is better


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

Dustin Campbell
Guest
 
Posts: n/a
#9: Jan 5 '07

re: Which method is better


Which method is better to remove a char from a string?
Quote:
>
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.


Christof Nordiek
Guest
 
Posts: n/a
#10: Jan 5 '07

re: Which method is better


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.

"Andrea" <googlegroups@spam.no.cleanmail.itschrieb im Newsbeitrag
news:F4EEFD9E-4300-4BE6-8EE3-A337FB0373B1@microsoft.com...
Quote:
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

Shah Ji
Guest
 
Posts: n/a
#11: Jan 6 '07

re: Which method is better


It depends on the number of strings you would like to work with if they
are less than 10 I would say use any method as they will not effect
much of the performance but if your application is working with 100's
of string every time than that is a different story.

Andrea wrote:
Quote:
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
Closed Thread