473,414 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Which method is better

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

Jan 4 '07 #1
10 1626
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::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.
Jan 4 '07 #2
Andrea <go**********@spam.no.cleanmail.itwrote:
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 - <sk***@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
Jan 4 '07 #3
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:
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
Jan 4 '07 #4
And the Remove mthod will be evne better than either the Replace or
SubString methods.
"Andrea" <go**********@spam.no.cleanmail.itwrote in message
news:F4**********************************@microsof t.com...
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

Jan 4 '07 #5
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Andrea <go**********@spam.no.cleanmail.itwrote:
>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
>
--
Jon Skeet - <sk***@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

Jan 5 '07 #6
Bill Butler <qw****@asdf.comwrote:
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 - <sk***@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
Jan 5 '07 #7
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

Jan 5 '07 #8
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.
Jan 5 '07 #9
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" <go**********@spam.no.cleanmail.itschrieb im Newsbeitrag
news:F4**********************************@microsof t.com...
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

Jan 5 '07 #10
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:
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
Jan 6 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
14
by: Blue Ocean | last post by:
My c++ text tells me that I should define methods this way: class Stack { int method(double t); Stack(int s); ... } int Stack::method(double t)
17
by: lawrence | last post by:
How is it possible that the question "How do I detect which browser the user has" is missing from this FAQ: http://www.faqts.com/knowledge_base/index.phtml/fid/125 and is only here on this...
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
2
by: Russell Hind | last post by:
I have a delegate which I use to store a current 'state' function (for a statemachine inside a form). __delegate void State_t(const Message_c& Message); I assign to it such as m_State = new...
4
by: Lerp | last post by:
Hi all, With regards to calling data from a database and filling in an editing form based on some query, which is the best (least intensive on processor) method for assigning the returned...
53
by: Jon S via DotNetMonster.com | last post by:
Hi all, I'm planning on developing an ASP.NET web site. I know both VB.NET and C# but am unsure on which would be more useful to develop an ASP.NET site with? Also I maybe looking to become a...
14
by: J.S. | last post by:
In a Windows Form application, which is the better method to concatenate large blocks of code? 1. Reading the text from text files. 2. Adding the text to the VB file itself? Thanks! J.S. ...
1
by: Frank [GOD] | last post by:
Let me set this up for y'all... I have 8 mySQL databases with over 100K records, which include a phone number field. I call these the storage tanks. They're labeled db1 - db8. Then I have 1...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.