Newer StringBuilder? How to set String to null safely?

R

raylopez99

I see that String and StringBuilder in C# / C++ do not have an easy
way to set a string to null or zero length, once it is instantiated.

Apparently some variant of the .NET languages do (reading between the
lines from another fragment I found).

So the problem is to do this quickly (of course it can be done in a
roundabout manner), using either StringBuilder or String. I'm using
C#.NET version 2.0 (Visual Studio 2005). It looks like possibly
there's a new string class introduced in version 3.0 that has
a .remove or .clear public method?!

Ray Lopez

//////////////////// start

StringBuilder myString = new StringBuilder("Hello");

myString.Append(" World!");

Console.WriteLine("write out your string: {0}", myString); //Hello
World!

myString = null; //the intent here is to reset the string to a "" or
zero length string; obviously this is not the way to do this, as you
get a run-time error

Console.WriteLine(myString==null); // you get output: "True", since
myString is now null.

// this next line won't work--gives a runtime error, so the question
is: how to reset your string?

// myString.Append("my new string text for myString here, but gives a
runtime error!");

//////////////////// end
 
G

Giovanni Dicanio

raylopez99 said:
I see that String and StringBuilder in C# / C++ do not have an easy
way to set a string to null or zero length, once it is instantiated.
StringBuilder myString = new StringBuilder("Hello");

myString.Append(" World!");
myString = null; //the intent here is to reset the string to a "" or
zero length string; obviously this is not the way to do this, as you
get a run-time error

I'm not expert of C#, but would it be possible to do just:

myString = new StringBuilder();


Giovanni
 
J

Jerry Coffin

I see that String and StringBuilder in C# / C++ do not have an easy
way to set a string to null or zero length, once it is instantiated.

There is no such thing as a StringBuilder in C++ (unless it's something
you've written yourself). It looks like you've been taken in by
Microsoft's misleading labeling -- they have something the call C++/CLI,
which seems like it should be C++, but really includes lots of things
that aren't parts of C++ at all (including something named
StringBuilder).

There's probably some newsgroup with a name like
microsoft.public.vc.language where this would be topical.
 
D

David Wilkinson

Jerry said:
There is no such thing as a StringBuilder in C++ (unless it's something
you've written yourself). It looks like you've been taken in by
Microsoft's misleading labeling -- they have something the call C++/CLI,
which seems like it should be C++, but really includes lots of things
that aren't parts of C++ at all (including something named
StringBuilder).

There's probably some newsgroup with a name like
microsoft.public.vc.language where this would be topical.

ray:

It is almost always a serious blunder to cross-post anything to both the
Microsoft and comp.lang groups. The folks at the latter are just too touchy (and
I think correctly so).

Even if your original question is culture-agnostic, the thread will invariably
stray into forbidden territory. In these cases, multi-posting (despite its evil
reputation) works better. [Multi-posting to different Microsoft groups is
however A Bad Thing.]

In this case, your question was out of line at comp.lang.c++ because it was a
question about C++/CLI, which is a different language from C++. You got the
right Microsoft group though :).
 
R

raylopez99

Perhaps not the appropriate group. You should likely try a C# group instead
such as microsoft.public.dotnet.languages.csharp

In addtion to creating a new stringbuilder, it looks like myString.Length=0
should work according to the documentation (myString being actually a
StringBuilder object).

Yes, StringBuilder.length = 0 looks like it might set the string to
zero length.

Thanks!

Sorry for the cross-post, but I did get a useful answer, which says
something...

RL
 

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