C++
  Home arrow C++ arrow Page 3 - 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 - Checks, find, concatenation


    (Page 3 of 4 )

    Checking to see if it contains any data can be done via two methods. The member function empty() will return a bool value, or you can also make a comparison with an empty string as follows.


    if(text == "")


    if(text.empty())


    Either of these will have the same result. With this, we have reached the problem of comparing two strings. The member function responsible for this is the compare(). Nevertheless, calling the operator == will call the comparison if the items can be cast on both sides or already are strings. The < and > operators are defined and will return which one is smaller by comparing the ASCI chars each after the other until it finds one smaller than the char at the same place in the other. Both examples below will return a true or false.


    string text, beta;

    text.compare(beta);

    text == "Yeti"; // here a string type will be created from the

    //"Yeti" (char*) and after that called the comparison function


    Concatenation of two streams is readable by doing it only with the addition operator, but you can insert a new string into a old one at any place given by the vastly different types of definition of the insert member function. Here is, for example, the concatenation in three ways. However, during the insert operation, the alfa will be extended to the concentration, but this can be changed by first assigning alfa to both, and then inserting beta into both.

    #include <iostream>

    #include <string>

    using namespace std;


    int main()

    {

    string alfa = "alfa ";

    string beta = "beta";

    string both;


    cout << (both = alfa + beta) << endl;

     

    both = alfa;

    both.insert(both.end(), beta.begin(), beta.end());

    cout << both << endl;


    cout << (both = alfa.insert(alfa.length(), beta)) << endl;


    return 1;

    }


    alfa beta

    alfa beta

    alfa beta

    Press any key to continue . . .


    Finding a string inside of another is always a hard task for newbie coders. Especially if they have a good knowledge of C, most people are searching for absurdly complicated names (strstr for example in C). The find functions does exactly what its name says; it searches the argument string and returns the position at where it finds it, or string::npos if it is not found.


    #include <iostream>

    #include <string>

    using namespace std;


    int main()

    {

    string alfa = "nalfa ";

    string beta = "alf";

    cout << alfa.find(beta) << endl;


    return 1;

    }


    1

    Press any key to continue . . .


    Of course, the list of finding methods is much larger, as the picture below indicates:


    More C++ Articles
    More By Gabor Bernat


       · String text ("Yeti); is missing a closing quote, and should readString text...
       · some of the code has missing or inappropriate line breaks with the // short...
       · For the quotes you are right. My mistake, sorry I am human too.As for the //...
     

    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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek