C++ String Class

◀ Character Array in C▶ Constructors
Amazon A C++ string class object is very easy to manipulate. For example, you can append it with another string, a character, or a pointer to characters with very little hassle. You can insert another string into a string. You can extract a part of the string given the beginning and ending position. You can erase a substring from a string.

Being aware of the methods string provides is very important because string is one of the most versatile types to store data.
Many programmers tend to use a struct or a class to store data, but is a structure or a class the only way to store data?


What about using a string with delimiters separating the data items? For example, let’s say that a bank customer has the following information: id, account creation date, account type, email, password, and balance, where each data item is either a number or a string. We can use a structure or a class to encapsulate all the data items.

On the other hand, we can also use a string, with a character being the delimiter, to represent a client. An example is (# is the delimiter): “3456#3/17/1979#prima#jo@yahoo.com#password#99999.99#”.
Then we create functions to handle parsing of this string and retrieving a particular data item of this customer. It usually is NOT a good idea but at least it’s a possibility and may come in handy in some situation.


A string comes in handy all the time. Whenever you need to store English characters you need string. Obviously you can use char array too but you’ll need to deal with the nitty gritty details of managing a char array such as appending characters to the char array. These details have been taken care for you already by the string class.

It’s a good idea to NOT reinvent the wheel in this situation. Use the tools that have already been tested by time to spare yourself the effort. You should focus your energies on the business logic of your software application!

◀ Character Array in C▶ Constructors

fShare
Questions? Let me know!