Chapter 14

Using Strings and Regular Expressions


  • The differences between C-style strings and C++ strings
  • How you can localize your applications to reach a worldwide audience
  • How to use regular expressions to do powerful pattern matching

Every program that you write will use strings of some kind. With the old C language there is not much choice but to use a dumb null-terminated character array to represent an ASCII string. Unfortunately, doing so can cause a lot of problems, such as buffer overflows, which can result in security vulnerabilities. The C++ STL includes a safe and easy-to-use string class that does not have these disadvantages.

The first section of this chapter discusses strings in more detail. It starts with a discussion of the old C-style strings, explains their disadvantages, and ends with the C++ string class. It also mentions raw string literals, which are new in C++11.

The second section discusses localization, which is becoming more and more important these days to allow you to write software that can be localized to different regions around the world.

The last section introduces the new C++11 regular expressions library, which makes it easy to perform pattern matching on strings. They allow you to search for sub-strings matching a given pattern, but also to validate, parse, and transform strings. They are really powerful and it’s recommended that you start using them instead of manually writing your own string processing code.


Get Professional C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.