C++
  Home arrow C++ arrow Page 4 - 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 / 3
    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 - Approaching the end


    (Page 4 of 4 )

    In addition, you also need to know how to delete items from a string. This can be done using the erase method. If you pass a single argument (that is of type int) to the erase member function, all of the string's content will be deleted until the end. When you pass a second argument, you will also indicate the number of characters to delete from the string. On the other hand, you can delete everything inside it with the clear() member function.


    std::string text("Yeti Developer");

    text.erase(4); //delete all until the end from char at pos 5 text.erase(0,2); //delete the first two characters

    cout << text;


    ti

    Press any key to continue . . .


    As good as the string class is, sometimes you may need to get a char* pointer to your data so you can call an already existing function with this as an argument. For this reason, and to maintain some backward compatibility with C, the c_str() and data() member functions  have been introduced. This will return a pointer like that for you. With it, you can call functions from the old C world (still, I do not recommend this habit).


    strstr(alfa.c_str(), beta.data()); // this will return for //both a char* pointer allowing you to use the function


    With this, you know enough of the string container so you can use it if you came from the world of C or just saw it for the first time. Another important question remains unanswered. How we put an int value inside a string? The answer is in the world of streams and will be covered in an upcoming article titled "Using Stringstreams." 

    I thank you for investing your time in developing yourself by reading this article, and remember that you can post your remarks in the blog section over here or join the DevHardware forums and do the same over there. Until next time, remember to: Live with Passion!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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-2010 by Developer Shed. All rights reserved. DS Cluster 8 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek