Formatting Strings

You can insert padding (white space) into strings so that each string in a group of strings has the same length. The center, leftJustify, and rightJustify functions all do this; their names indicate where they place the existing string relative to the added white space. You provide the final desired length of the string, and the function adds the correct amount of white space (or removes characters if the string is longer than the final length you specify). For example:

// Padding IStrings
#include <istring.hpp>
#include <iostream.h>
void main() {
   IString s1="Short", s2="Not so short",
           s3="Too long to fit in the desired field length"; 
   s1.rightJustify(20);
   s2.center(20);
   s3.leftJustify(20);
   cout << s1 << '\n' << s2 << '\n' << s3 << endl;
   } 

This program produces the following output:

               Short
    Not so short
Too long to fit in t

If a string is too wide, you can strip leading or trailing blanks using the strip... functions:

// Using the strip... functions of IString
#include <istring.hpp>
#include <iostream.h>
void main() {
   IString s1, s2, s3, Long=" Lots of space here ";
   s1 = s2 = s3 = Long; 
   s1.stripLeading();
   s2.stripTrailing();
   s3.strip();
   cout << ">" << Long << "<\n"
        << ">" << s1 << "<\n"
        << ">" << s2 << "<\n"
        << ">" << s3 << "<" << endl;
   } 

This program produces the following output:

>      Lots of space here     <
>Lots of space here     <
>      Lots of space here<
>Lots of space here<

You can also change the case of an IString to all uppercase or all lowercase:

// Changing the case of IStrings
#include <iostream.h>
#include <istring.hpp>
void main() {
   IString Upper="MANY of THESE are UPPERCASE CHARACTERS";
   IString Lower="Many of these ARE lowercase characters"; 
   Upper.change("MANY","NONE").lowerCase();
   Lower.change("Many","None").upperCase();
   cout << Upper << '\n' << Lower << endl;
   } 

This program produces the following output:

   none of these are uppercase characters
   NONE OF THESE ARE LOWERCASE CHARACTERS