Question What is the latest and greatest way of doing strings in .Net VB / C++ / C# ?

amakeler

Member
Joined
Dec 23, 2008
Messages
7
Programming Experience
10+
Hi all,

RE: What is the latest and greatest way of doing strings in .Net VB / C++ / C# ?

Using Visual Express 2008.

I am designing an "Intro to Programming" course for tech writers.

I am writing sample code in a number of languages.

Pray tell me what is the latest and greatest way of doing strings in VB / C++ / Net?

Is it the .Net class, String, as in:

String Class (System)

or how about

"New Recommendations for Using Strings in Microsoft .NET 2.0"
New Recommendations for Using Strings in .NET 2.0
(Does the Recommendation change between .Net 2.0 and .Net 3.5?)

?

Once upon a time, VB did strings its own way, C did strings in its own way, C++ did strings in its own way.

Now that .Net has come along, they are all supposed to do it in the same way. Right?

And I seem to notice there is a String class and a Strings class. Difference?


Tia

- avi
 
Last edited:
Use the Stringbuilder class.

The following was taken from the Help file. Search for Stringbuilder and check it out for several examples. It works with all languages in the .NET framework:


The String object is immutable. Every time you use one of the methods in the System.String class, you create a new string object in memory, which requires a new allocation of space for that new object. In situations where you need to perform repeated modifications to a string, the overhead associated with creating a new String object can be costly. The System.Text.StringBuilder class can be used when you want to modify a string without creating a new object. For example, using the StringBuilder class can boost performance when concatenating many strings together in a loop.

You can create a new instance of the StringBuilder class by initializing your variable with one of the overloaded constructor methods.
 
Thanks, Solitaire. I am glad I know that now.

But I will use the simpler class for the basic seminar I have to give.

Thanks!

- avi
 
Actually, since the Seminar should represent the up-to-date way of doing things, I should present StringBuilder.

Actully, if I present both ways (also VB6 strings) it will be a good way of presenting the difference between OOP and procedural prog.

Tnx again,

- avi
 
"the difference between OOP and procedural prog" is what you find between the Strings module methods (VB6 leftovers basically) and the String class. Example:
VB.NET:
Dim s As String = "hello"
Dim proc As String = Strings.Mid(s, 2, 1)
Dim oop As String = s.Substring(2, 1)
You see the Mid example uses an external method that is not related to the hello string object, the Substring example uses a internal method of the String class that relates directly to the hello string object. Both methods return a new string object.

"StringBuilder vs String" is "immutable vs mutable" as explained by Solitaire, the builder just helps the building process of the string.
 
Back
Top