Home arrow C++ arrow Page 4 - The STL String Class
C++

The STL String Class


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.

Author Info:
By: Gabor Bernat
Rating: 5 stars5 stars5 stars5 stars5 stars / 9
March 31, 2009
TABLE OF CONTENTS:
  1. · The STL String Class
  2. · Read or print using string
  3. · Checks, find, concatenation
  4. · Approaching the end

print this article
SEARCH DEVARTICLES

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.

blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- 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++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials