Link to home
Start Free TrialLog in
Avatar of sneeuw
sneeuwFlag for Belgium

asked on

(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
Avatar of irisistable
irisistable

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*.
> 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).

ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.  
> Opps.  1 minute too late.  I'd reject if I could

don't let Axter here about this... :)
>> 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.
I'm, sure we'll here about it!
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.
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.
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.
nietod:
Good point - I'd misread the question, and thought sneeuw wanted to convert between them.
i'm sorry, my computer lost control...
typedef basic_string<char> string;
typedef basic_string<wchar_t> wstring;

did I get there first?
> 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;

>> 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.
too many cooks!
Avatar of sneeuw

ASKER

Will string work with MBCS (MultiByte Character Set) too ?
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.
Avatar of sneeuw

ASKER

>> 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
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.
Avatar of Axter
>>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.