(Ansi)String Class

Hi,

I use Borland cpp Builder and Borland's components all rely on the AnsiString class which can hold a text string.
This class incorporates a lot of easy to use functions to work with text strings.  E.g. a string can also grow etc. String A = B + C ; // B and C are strings too.

In my code (engine stuff, not GUI) I turn to char* and functions such as strlen(), strncpy() ect.
I mainly do this to not have to rely on Borland specific implementations, to easily port to other environments etc..

I'm now doubting if that is the correct approach as there are lots of limitations (e.g. the strings can not grow etc. ...).

The question ... do other environments (e.g. Visual C) also use a class called AnsiString and is the same functionality supported (because if it is ... this String class is a powerfull feature) or are there other ways ... or is it best to work with the basic stuff char* strnlen() etc ...
 
Peter
sneeuwAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
nietodConnect With a Mentor Commented:
>> I'm now doubting if that is the correct approach as there are lots of limitations
Don't doubt it.  It is a mistake.   Not only are NUL-terminated (C-style) strings limited, but more importantly they are dangerious!  They are considered to be the leading cause of bugs in C programs..   You can easily store a string that is too long into an array and corrupt other memory or forget to place a NUL terminator and cause a string to go awry.

Defintiely avoid C-strings unless absolutely necessary (like when interfacing with C code, or OS-code).

>>  do other environments (e.g. Visual C) also use a class called AnsiString
No.   AnsiString is BCB-speciic.  But standard C++ has a very poserful standard string class called "string"--very creative name)   This is the string class you should use.
0
 
irisistableCommented:
in STL (Standard Template Library), which always comes with C++, there is a class called string
#include <string>
which supplies all what you need.
look at a manual (like MSDN) and find out the methods of this class, which replace methods like strlen() which you would have used if you were using the simple char*.
0
 
jasonclarkeCommented:
> are there other ways

the right thing to do is use the std::string class, since this is standard C++ and should be available on any modern C++ compiler.

Even if Borland C++ does not have it, you could try the stlport version (www.stlport.org).

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
IainHereCommented:
I was busy typing a comment very similar to the others, and I just thought I'd check that I wasn't being beaten to it... I was :)

I agree with everything they say.  BCB DOES have the STL as standard - look at this link

http://community.borland.com/soapbox/techvoyage/article/1,1795,10284,00.html

about 2/3 of the way down it says:
Note: The AnsiString class is similar to the STL string class, but it specifically designed to enhance the unique features of the VCL.

There is some info about using the STL in the context of the VCL.  Good luck.
0
 
nietodCommented:
Opps.  1 minute too late.  I'd reject if I could.  sneew, you may want to reject my answer for an earlier one

Anyways, the standard string class is defined in <string> and will be very similar to AnsiiString.  If you need to obtain a char * string from an STL string, you can use the c_str() member function to obtain it.  However, you the string data that the c_str() function returns a pointer to is of a temporary nature.  it may be changed or destoyed the next time a non-constant member function is called on the string.  So you should not "save" this pointer, use it right away or if absolutely necessary copy the string data to an array.

the STL string class has soem powerful feature syou won't find in AnsiString.  First of all, it is a template class.  This lets you create strings that store elements of non-"char" type, like wchar_t.  It also allows yoiu to define strings that use customer allocators.   another difference is that the string class is an STL container.  it supports iterators like the other containers.  it can be used in STL algorithms, like other containers.   This makes it very powerful.  
0
 
jasonclarkeCommented:
> Opps.  1 minute too late.  I'd reject if I could

don't let Axter here about this... :)
0
 
nietodCommented:
>> Note: The AnsiString class is similar to the STL string class, but it
>> specifically designed to enhance the unique features of the VCL.
That's not likely to be a problem in a non--GUI application.   I.e. I get the impression that the application is not using VCL, at least not extensively.  For general string operations the STL string class will be just as good--probably better if you really know STL.  This would only be a problem if you are interfacing with VCL-specific code that is expectng AnsiString parameters.
0
 
nietodCommented:
I'm, sure we'll here about it!
0
 
irisistableCommented:
nietod, please correct me if i am wrong - i think that class string from STL is *only* for strings of char type, not wchar_t. i think that bstring is the base template class, and it is good for wchar_t, and class string was created just for the purpose of type chat, becasue it is so needed and so common.
0
 
irisistableCommented:
nietod, please correct me if i am wrong - i think that class string from STL is *only* for strings of char type, not wchar_t. i think that bstring is the base template class, and it is good for wchar_t, and class string was created just for the purpose of type chat, becasue it is so needed and so common.
0
 
irisistableCommented:
nietod, please correct me if i am wrong - i think that class string from STL is *only* for strings of char type, not wchar_t. i think that bstring is the base template class, and it is good for wchar_t, and class string was created just for the purpose of type chat, becasue it is so needed and so common.
0
 
IainHereCommented:
nietod:
Good point - I'd misread the question, and thought sneeuw wanted to convert between them.
0
 
irisistableCommented:
i'm sorry, my computer lost control...
0
 
IainHereCommented:
typedef basic_string<char> string;
typedef basic_string<wchar_t> wstring;

did I get there first?
0
 
jasonclarkeCommented:
> i think that class string from STL is *only* for strings of
> char type

yes, string is actually a typedef, specialising the basicstring template for simple chars:

typedef basic_string<char, char_traits<char>, allocator<char> > string;

there is an equivalent for wchar_t

typedef basic_string<wchar_t, char_traits<wchar_t>,
     allocator<wchar_t> > wstring;

0
 
nietodCommented:
>> i think that class string from STL is *only* for strings of
>> char type, not wchar_t. i think that bstring is the base
>> template class,
That is correct.   the template class is called basic_string<>.  Then the class "string" is a specialization of basic_string for type "char" and the default allocator (new and delete).

But basically this is a fine point, the two have the exact same interface, since string is a specialization.  But it is a good point since if you try to declare string<> as a template of other types, it won't work, since basic_string is the template.
0
 
nietodCommented:
too many cooks!
0
 
sneeuwAuthor Commented:
Will string work with MBCS (MultiByte Character Set) too ?
0
 
nietodCommented:
yes and no.   MBCS stores data as 8 bit characters, so you could store an MBCS string in a string object--just like you can sotre it in an array of characters.   however, it doesn't recongnize the fact that some of the characters inside the string object are larger than a 8 bits.  So for example, indexing operations that use [] will not necessarily work correctly, i.e. the might return part of a charcter, not an entire character, and the offset value passed in the [] will be in terms of bytes, not characters.   however these same limitations are true of storing an MBCS string in an ordinary character array as well.

If you store an MBCS string in a string object, you can use windows MBCS functions to manipulate it, but its going to be a bit awkward at times.

however do you really need MBCS strings?  Wide characters (unicode for example) are prefered by both the windows OS and the STL library.
0
 
sneeuwAuthor Commented:
>> however do you really need MBCS strings?  Wide characters (unicode for example) are prefered by both
the windows OS and the STL library.

Well ...
I get the texts in either 8 bits/char Or Unicode.
In case of Unicode I now convert to MBCS because VCL's AnsiString uses MBCS which in terms is done to be able to cope with Win95.  Suppose I were to change to unicode : To be able to deal with certain foreign languages I would then have to convert all 8 bit characters to unicode.  When displayed (using VCL) all would have to be converted again.  I'm not sure what to do
0
 
nietodCommented:
If you are working with VCL reasonbly extensively, then your probably shoudl be using AnsiString.  I would use STL string only if there is little or no interfaceing with STL.

If you want your code to be more portable, then you can learn about the STL string class and use only the portiosn of the STL string class and the AnsiString class that are the same.  i.e. both have operator [] so you can use that.   But AnsiString does not have push_first, so don't use that.   and so one.   Then rather than using AnsiString or STl String directly, use a typedef that defines one string or the other, so you an change which string you are actually using by changing a single typedef.  if there are features of the two strings that are not idential, you cna write a wrapper function that performs that feature, then you can call that wrapper function throughout your code, this this helps to isolate the string-dependant portions of the program.
0
 
AxterCommented:
>>don't let Axter here about this... :)

>>I'm, sure we'll here about it!

I don't see a problem if expert acknowledges mistake.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.