<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>ChapterΒ 13.Β String Classes</title><meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /><meta name="keywords" content=" ISO C++ , library " /><link rel="start" href="../spine.html" title="The GNU C++ Library Documentation" /><link rel="up" href="strings.html" title="PartΒ V.Β Strings" /><link rel="prev" href="strings.html" title="PartΒ V.Β Strings" /><link rel="next" href="bk01pt05ch13s02.html" title="Case Sensitivity" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">ChapterΒ 13.Β String Classes</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="strings.html">Prev</a>Β </td><th width="60%" align="center">PartΒ V.Β Strings</th><td width="20%" align="right">Β <a accesskey="n" href="bk01pt05ch13s02.html">Next</a></td></tr></table><hr /></div><div class="chapter" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title"><a id="manual.strings.string"></a>ChapterΒ 13.Β String Classes</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="bk01pt05ch13.html#strings.string.simple">Simple Transformations</a></span></dt><dt><span class="sect1"><a href="bk01pt05ch13s02.html">Case Sensitivity</a></span></dt><dt><span class="sect1"><a href="bk01pt05ch13s03.html">Arbitrary Character Types</a></span></dt><dt><span class="sect1"><a href="bk01pt05ch13s04.html">Tokenizing</a></span></dt><dt><span class="sect1"><a href="bk01pt05ch13s05.html">Shrink to Fit</a></span></dt><dt><span class="sect1"><a href="bk01pt05ch13s06.html">CString (MFC)</a></span></dt></dl></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="strings.string.simple"></a>Simple Transformations</h2></div></div></div><p>Here are Standard, simple, and portable ways to perform commontransformations on a <code class="code">string</code> instance, such as"convert to all upper case." The word transformationsis especially apt, because the standard template function<code class="code">transform<></code> is used.</p><p>This code will go through some iterations. Here's a simpleversion:</p><pre class="programlisting">#include <string>#include <algorithm>#include <cctype> // old <ctype.h>struct ToLower{char operator() (char c) const { return std::tolower(c); }};struct ToUpper{char operator() (char c) const { return std::toupper(c); }};int main(){std::string s ("Some Kind Of Initial Input Goes Here");// Change everything into upper casestd::transform (s.begin(), s.end(), s.begin(), ToUpper());// Change everything into lower casestd::transform (s.begin(), s.end(), s.begin(), ToLower());// Change everything back into upper case, but store the// result in a different stringstd::string capital_s;capital_s.resize(s.size());std::transform (s.begin(), s.end(), capital_s.begin(), ToUpper());}</pre><p><span class="emphasis"><em>Note</em></span> that these calls allinvolve the global C locale through the use of the C functions<code class="code">toupper/tolower</code>. This is absolutely guaranteed to work --but <span class="emphasis"><em>only</em></span> if the string contains <span class="emphasis"><em>only</em></span> charactersfrom the basic source character set, and there are <span class="emphasis"><em>only</em></span>96 of those. Which means that not even all English text can berepresented (certain British spellings, proper names, and so forth).So, if all your input forevermore consists of only those 96characters (hahahahahaha), then you're done.</p><p><span class="emphasis"><em>Note</em></span> that the<code class="code">ToUpper</code> and <code class="code">ToLower</code> function objectsare needed because <code class="code">toupper</code> and <code class="code">tolower</code>are overloaded names (declared in <code class="code"><cctype></code> and<code class="code"><locale></code>) so the template-arguments for<code class="code">transform<></code> cannot be deduced, as explained in<a class="ulink" href="http://gcc.gnu.org/ml/libstdc++/2002-11/msg00180.html" target="_top">thismessage</a>.At minimum, you can write short wrappers like</p><pre class="programlisting">char toLower (char c){return std::tolower(c);} </pre><p>The correct method is to use a facet for a particular localeand call its conversion functions. These are discussed more inChapter 22; the specific part is<a class="ulink" href="../22_locale/howto.html#7" target="_top">Correct Transformations</a>,which shows the final version of this code. (Thanks to James Kanzefor assistance and suggestions on all of this.)</p><p>Another common operation is trimming off excess whitespace. Muchlike transformations, this task is trivial with the use of string's<code class="code">find</code> family. These examples are broken into multiplestatements for readability:</p><pre class="programlisting">std::string str (" \t blah blah blah \n ");// trim leading whitespacestring::size_type notwhite = str.find_first_not_of(" \t\n");str.erase(0,notwhite);// trim trailing whitespacenotwhite = str.find_last_not_of(" \t\n");str.erase(notwhite+1); </pre><p>Obviously, the calls to <code class="code">find</code> could be inserted directlyinto the calls to <code class="code">erase</code>, in case your compiler does notoptimize named temporaries out of existence.</p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="strings.html">Prev</a>Β </td><td width="20%" align="center"><a accesskey="u" href="strings.html">Up</a></td><td width="40%" align="right">Β <a accesskey="n" href="bk01pt05ch13s02.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">PartΒ V.Β StringsΒ </td><td width="20%" align="center"><a accesskey="h" href="../spine.html">Home</a></td><td width="40%" align="right" valign="top">Β Case Sensitivity</td></tr></table></div></body></html>