PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Newer StringBuilder? How to set String to null safely?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Newer StringBuilder? How to set String to null safely?

Réponse
 
LinkBack Outils de la discussion
Vieux 02/07/2008, 15h11   #1
raylopez99
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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

  Réponse avec citation
Vieux 02/07/2008, 16h04   #2
Giovanni Dicanio
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newer StringBuilder? How to set String to null safely?


"raylopez99" <raylopez99@yahoo.com> ha scritto nel messaggio
news:33bb43cf-5578-436a-ac14-adb1dc4bfb8e@a1g2000hsb.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


  Réponse avec citation
Vieux 02/07/2008, 16h06   #3
Jerry Coffin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newer StringBuilder? How to set String to null safely?

In article <33bb43cf-5578-436a-ac14-
adb1dc4bfb8e@a1g2000hsb.googlegroups.com>, raylopez99@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.
  Réponse avec citation
Vieux 02/07/2008, 18h07   #4
Patrice
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newer StringBuilder? How to set String to null safely?

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" <raylopez99@yahoo.com> a écrit dans le message de groupe de
discussion :
33bb43cf-5578-436a-ac14-adb1dc4bfb8e...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
>


  Réponse avec citation
Vieux 02/07/2008, 19h54   #5
raylopez99
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newer StringBuilder? How to set String to null safely?

On Jul 2, 10:07am, "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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h02.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14977 seconds with 13 queries