How To Make Full Use Of Strings Everywhere In Your Code

It’s hard to write anything without using text. You’ll need to display text, read and write text to files, read the text typed on keyboards, and use text in other places that your users will never even notice. It’s everywhere. So it makes sense that C++ would provide a lot of support for text, right?

Not in the beginning. The C++ language was sometimes called C with classes for a reason. It started out based on the C language. When I first started learning C++ in the early 1990’s, many people thought it was just a better version of C. That was a common perception.

One big problem that contributed to this was no support for a string class. Maybe since C doesn’t have a string type, it was thought that C++ could get by without it too? I don’t know. What I do know is that this one missing capability was a big problem.

Both C and C++ have pointers and that means they can both declare pointers to a char type. This is all you need in C to describe a string. Just point to the first character in the string.

How do you know how long the string is? Easy, just keep advancing through memory looking at each character until you find one with the value zero. This is the null character and means you reached the end of the string. Many viruses take advantage of this by causing your application to miss the null character and go past the end of the string without realizing it.

Because C doesn’t have classes, it gives you several methods to work with the pointers. And C++ started out using the same methods.

Sometime in the mid 1990’s Microsoft released a rather large and comprehensive library called MFC. This let programmers write native Windows applications in C++ that supported many of the fancy features of that time such as multiple document interfaces. One of the most eagerly used classes in MFC was a string class called CString.

CString became so popular that many applications would use MFC just so they could get a string class. That’s all they really wanted.

The biggest problem with CString was that it was available only in MFC. C++ is supposed to be portable to many platforms, not just Windows. Okay, it had another problem too. It was big and clunky.

I talk about CString in the past tense because you really shouldn’t need to consider it anymore. You now have a standard and easy-to-use class for working with strings. It’s built into every C++ compiler’s standard library now and there’s no reason not to use std::string. It’s been available since the 1998 version of C++. I’ll just refer to it as string from now on.

Probably the biggest advantage of using string is that you can now work with a real class that manages the string for you and gives you access to the normal operators.

So if you want to append one string to the end of another string, you don’t need to call raw methods anymore. You can just use the + operator and get back a new string that contains both the first part and the second part that was added to the end. It’s simple and natural and feels like C++.

This class will show you:

  • How to create string instances and give them values.
  • How to change strings to get new strings. Even if the new strings are longer than the originals.
  • How to compare strings so you know which order the strings can be sorted.
  • How to convert strings to and from numbers.
  • How to find characters within strings. There are some things you need to be careful with here or you might not find what you expect. You’ll learn about a special value called npos.
  • How to use strings to read input from cin and output to cout.
  • How to access individual characters.

Find Available Dates

Once you purchase one of the classes, you can reserve a spot in any of the upcoming dates for that class. Use the button below to easily search for the next available dates.

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

You may also like…