C++
  Home arrow C++ arrow The STL String Class
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

The STL String Class
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2009-03-31

    Table of Contents:
  • The STL String Class
  • Read or print using string
  • Checks, find, concatenation
  • Approaching the end

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    The STL String Class


    (Page 1 of 4 )

    A text is nothing more than a successive sequence of characters. In the old days of programming, text was generally stored inside an array of characters. However, working with such arrays proved to be problematic, as you could easily trigger an overflow of the pre-allocated buffer or call for an invalid index. Fortunately, there is an easier way to handle this today, which you will learn in this article.

    Aware of this troublesome glitch inside the first "mature" programming languages (C, Pascal), the designers of new, improved languages started to look for an easy and convenient solution. The way out was provided with object-oriented programming, first present in the C++ programming language designed by Bjarne Stroustrup.

    OOP takes away all of the buffer handling from the programmer and completes it internally. When you add new text, it will resize and extend as needed. Furthermore, it offers some functions that will also do some input validity checks while not taking away the old tools. The operator overloads capability of these languages assures one easy and user-friendly usage for it.

    Initially developed as a separate class, later, during the standardization process of the C/C++ language, this became the part of the STL library. As its base is just a container that holds characters inside. However, what sets it apart from the std::vector<char> declaration are the additional functions available for this class.

    The std::string declaration is in fact just an instantiation of the template class std::basic_string with the argument for char. There is a type definition for wide characters also with the name wstring if you prefer the Unicode way.

    Basic usage of it is as simple as it can be. Make sure you include the string header, and to avoid calling each member of the std namespace explicitly, add one of the following:


    using namespace std; // can be used all of the STL namespace

    //that is included before this

    using std::string; // particulate version of the upper, only // for the string container


    Now you can declare a new type that you may initialize with one of the constructor types. Be aware that using the assign operator immediately at the declaration process will lead the constructor to be called with the respective parameter.


    String text ("Yeti);

    // is the same as

    string text = "Yeti";


    Nevertheless, in the lines below this, the assign operator will take care of this. Because of this, it can be assumed that the following line will throw an error:


    string alfa('a'); //or string alfa = 'a';


    However, for the assign operator this is a valid operation, and you can assign a single char to a string. So just split the upper code as follows and it will work:


    string alfa;

    alfa = 'a';


    There are additional ways to set the content of a string by using the assign function. This will accept a char* pointer ( the "Yeti" will return a const char pointer), or another definition exists that requires two arguments. The second tells the char what to use to fill the string, while the first points out how many times that should be replicated inside the string.


    string alfa;


    alfa.assign("Yeti"); // the string will contain Yeti

    alfa.assign(3, 'a'); // the string will contain aaa


    More C++ Articles
    More By Gabor Bernat


     

    C++ ARTICLES

    - More Tricks to Gain Speed in Programming Con...
    - Easy and Efficient Programming for Contests
    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT