Padding a String

Problem

You need to "pad," or fill, a string with a number of occurrences of some character to a certain width. For example, you may want to pad the string "Chapter 1" to 20 characters wide with periods, so that it looks like "Chapter 1...........".

Solution

Use string's insert and append member functions to pad a string with characters on the beginning or end. For example, to pad the end of a string to 20 characters with X's:

std::string s = "foo";
s.append(20 - s.length( ), 'X');

To pad the string at the beginning instead:

s.insert(s.begin( ), 20 - s.length( ), 'X');

 

Discussion

The difference in usage between the two functions is insert's first parameter. It is an iterator that points to the character immediately to the right of where the insert should occur. The begin member function returns an iterator pointing to the first element in the string, so in the example, the series of characters is inserted to the left of that. The parameters common to both functions are the number of times to repeat the character and the character itself.

insert and append are actually member functions of the basic_string class template in the header (string is a typedef for basic_string and wstring is a typedef for basic_string), so they work for strings of narrow or wide characters. Using them as needed, as in the above example, will work fine, but if you are using basic_string member functions from within your own generic utility functions, you should build on the standard library's existing generic design and use a function template. Consider the code in Example 4-1, which defines a generic pad function template that operates on basic_strings.

Example 4-1. A generic pad function template

#include 
#include 

using namespace std;

// The generic approach
template
void pad(basic_string& s,
 typename basic_string::size_type n, T c) {
 if (n > s.length( ))
 s.append(n - s.length( ), c);
}

int main( ) {

 string s = "Appendix A";
 wstring ws = L"Acknowledgments"; // The "L" indicates that
 // this is a wide char
 pad(s, 20, '*'); // literal
 pad(ws, 20, L'*');

 // cout << s << std::endl; // You shouldn't be able to
 wcout << ws << std::endl; // run these at the same time
}

pad in Example 4-1 pads the given string s up to some width n, with the character c. Since the function template uses a parameterized type for the elements of the string (T), it will work on a basic_string of any kind of character: char, wchar_t, or other custom characters.

Building C++ Applications

Code Organization

Numbers

Strings and Text

Dates and Times

Managing Data with Containers

Algorithms

Classes

Exceptions and Safety

Streams and Files

Science and Mathematics

Multithreading

Internationalization

XML

Miscellaneous

Index



C++ Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2006
Pages: 241

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net