Solved

(Ansi)String Class

Posted on 2001-07-02
22
538 Views
Last Modified: 2013-12-14
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
0
Comment
Question by:sneeuw
  • 8
  • 5
  • 3
  • +3
22 Comments
 

Expert Comment

by:irisistable
ID: 6244691
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
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6244693
> 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
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 6244697
>> 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
 
LVL 4

Expert Comment

by:IainHere
ID: 6244705
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
 
LVL 22

Expert Comment

by:nietod
ID: 6244712
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
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6244717
> Opps.  1 minute too late.  I'd reject if I could

don't let Axter here about this... :)
0
 
LVL 22

Expert Comment

by:nietod
ID: 6244720
>> 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
 
LVL 22

Expert Comment

by:nietod
ID: 6244723
I'm, sure we'll here about it!
0
 

Expert Comment

by:irisistable
ID: 6244726
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
 

Expert Comment

by:irisistable
ID: 6244730
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
 

Expert Comment

by:irisistable
ID: 6244732
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Expert Comment

by:IainHere
ID: 6244734
nietod:
Good point - I'd misread the question, and thought sneeuw wanted to convert between them.
0
 

Expert Comment

by:irisistable
ID: 6244735
i'm sorry, my computer lost control...
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6244746
typedef basic_string<char> string;
typedef basic_string<wchar_t> wstring;

did I get there first?
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6244747
> 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
 
LVL 22

Expert Comment

by:nietod
ID: 6244754
>> 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
 
LVL 22

Expert Comment

by:nietod
ID: 6244758
too many cooks!
0
 

Author Comment

by:sneeuw
ID: 6245949
Will string work with MBCS (MultiByte Character Set) too ?
0
 
LVL 22

Expert Comment

by:nietod
ID: 6246002
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
 

Author Comment

by:sneeuw
ID: 6246052
>> 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
 
LVL 22

Expert Comment

by:nietod
ID: 6246071
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
 
LVL 30

Expert Comment

by:Axter
ID: 6246390
>>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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now