473,406 Members | 2,390 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,406 software developers and data experts.

Newer StringBuilder? How to set String to null safely?

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

Jul 2 '08 #1
5 4052
In article <33bb43cf-5578-436a-ac14-
ad**********@a1g2000hsb.googlegroups.com>, ra********@yahoo.com says...
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 2 '08 #2

"raylopez99" <ra********@yahoo.comha scritto nel messaggio
news:33**********************************@a1g2000h sb.googlegroups.com...
>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
Jul 2 '08 #3
Jerry Coffin wrote:
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 :-).

--
David Wilkinson
Visual C++ MVP
Jul 2 '08 #4
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).

For a String I'm not sure what is your problem. I believe you may have some
confusion between the string and the stringbuilder you called myString ?...
--
Patrice
"raylopez99" <ra********@yahoo.coma écrit dans le message de groupe de
discussion :
33**********************************...oglegroups.com...
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
Jul 2 '08 #5
On Jul 2, 10:07*am, "Patrice" <http://www.chez.com/scribe/wrote:
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

Jul 2 '08 #6

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

Similar topics

51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
5
by: Andrea | last post by:
Hy I have a litle problem : I have to invoke a function in an unmanaged dll (writen in C) from C# ------------------------------------------------ int string_modify(unsigned char*...
3
by: Salvador | last post by:
Hi, I am using an old Win32 DLL that expects a LPBYTE as a parameter. The Stringbuilder appears to be working, the problem is that the component is sending me a string with NULL characters in the...
5
by: Heribert Coumans | last post by:
given the following 2 functions: public bool AnOtherSimpleTest1(string aString) { StringBuilder strB1 = new StringBuilder(); StringBuilder strB2 = new StringBuilder(); strB1.Append(aString);...
3
by: Mariano | last post by:
Hi, Can a StringBuilder buffer be cleared? I'm confused. It is supposed that the StringBuilder class, can deal with all the memory allocation and deallocation needed for normal string operations...
8
by: Henning M | last post by:
Hi, I'm trying to use stringbuilder to collect a list of strings. (as suggested by Claes Bergefall) Declare Auto Function CM_Get_Device_ID_List Lib "cfgmgr32.dll" (ByVal pszFilter As String,...
12
by: Richard Lewis Haggard | last post by:
I thought that the whole point of StringBuilder was that it was supposed to be a faster way of building strings than string. However, I just put together a simple little application to do a...
8
by: Arjan | last post by:
Hello, I have been looking for a couple of days now, but I can't find anything about how to deal with StringBuilder and releasing the memory used by it. When I use stringbuilder, the memory...
4
by: raylopez99 | last post by:
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...
34
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.