473,503 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
4 2066
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
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 #4
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 #5

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

Similar topics

51
8207
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
7128
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*...
5
1672
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
3504
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
2250
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
2689
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
2894
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...
5
4057
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
3487
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
7198
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,...
1
6979
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
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5570
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
4666
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...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.