<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
C++ Strings [C++ Reference]
</title>
<meta name="generator" content="DokuWiki Release 2009-12-25c "Lemming"" />
<meta name="robots" content="index,follow" />
<meta name="date" content="2009-10-15T09:27:10-0700" />
<meta name="keywords" content="string,start" />
<link rel="search" type="application/opensearchdescription+xml" href="/wiki/lib/exe/opensearch.php" title="C++ Reference" />
<link rel="start" href="/wiki/" />
<link rel="contents" href="/wiki/string/start?do=index" title="Index" />
<link rel="alternate" type="application/rss+xml" title="Recent Changes" href="/wiki/feed.php" />
<link rel="alternate" type="application/rss+xml" title="Current Namespace" href="/wiki/feed.php?mode=list&ns=string" />
<link rel="edit" title="Edit this page" href="/wiki/string/start?do=edit" />
<link rel="alternate" type="text/html" title="Plain HTML" href="/wiki/_export/xhtml/string/start" />
<link rel="alternate" type="text/plain" title="Wiki Markup" href="/wiki/_export/raw/string/start" />
<link rel="stylesheet" media="all" type="text/css" href="/wiki/lib/exe/css.php?s=all&t=custom1&tseed=1272971091" />
<link rel="stylesheet" media="screen" type="text/css" href="/wiki/lib/exe/css.php?t=custom1&tseed=1272971091" />
<link rel="stylesheet" media="print" type="text/css" href="/wiki/lib/exe/css.php?s=print&t=custom1&tseed=1272971091" />
<script type="text/javascript" charset="utf-8" ><!--//--><![CDATA[//><!--
var NS='string';var JSINFO = {"id":"string:start","namespace":"string"};
//--><!]]></script>
<script type="text/javascript" charset="utf-8" src="/wiki/lib/exe/js.php?tseed=1272971091" ></script>
<link rel="shortcut icon" href="/wiki/lib/tpl/custom1/images/favicon.png" />
</head>
<body>
<div class="dokuwiki">
<div class="stylehead">
<div class="header">
<div class="pagename">
[[<a href="../string/start.html" title="Backlinks">C++ Strings</a>]]
</div>
<div class="logo">
<div class="clearer"></div>
</div>
<div class="breadcrumbs">
<span class="bchead">You are here: </span><a href="../start.html" title="start">C++ Reference</a> » <a href="../string/start.html" title="string:start">C++ Strings</a> </div>
</div>
<div class="plugin_translation"><span>Translations of this page<sup><a href="../localization.html" class="wikilink1" title="localization">?</a></sup>:</span> <ul> <li><div class="li"><span class="curid"><a href="../string/start.html" class="wikilink1" title="string:start">en</a></span></div></li> <li><div class="li"><a href="../br-pt/string/start.html" class="wikilink1" title="br-pt:string:start">br-pt</a></div></li> <li><div class="li"><a href="../cn/string/start.html" class="wikilink1" title="cn:string:start">cn</a></div></li> <li><div class="li"><a href="../cz/string/start.html" class="wikilink2" title="cz:string:start" rel="nofollow">cz</a></div></li> <li><div class="li"><a href="../de/string/start.html" class="wikilink2" title="de:string:start" rel="nofollow">de</a></div></li> <li><div class="li"><a href="../es/string/start.html" class="wikilink2" title="es:string:start" rel="nofollow">es</a></div></li> <li><div class="li"><a href="../fr/string/start.html" class="wikilink1" title="fr:string:start">fr</a></div></li> <li><div class="li"><a href="../it/string/start.html" class="wikilink2" title="it:string:start" rel="nofollow">it</a></div></li> <li><div class="li"><a href="../jp/string/start.html" class="wikilink1" title="jp:string:start">jp</a></div></li> <li><div class="li"><a href="../nl/string/start.html" class="wikilink2" title="nl:string:start" rel="nofollow">nl</a></div></li> <li><div class="li"><a href="../pl/string/start.html" class="wikilink1" title="pl:string:start">pl</a></div></li> <li><div class="li"><a href="../ro/string/start.html" class="wikilink2" title="ro:string:start" rel="nofollow">ro</a></div></li> <li><div class="li"><a href="../ru/string/start.html" class="wikilink1" title="ru:string:start">ru</a></div></li> <li><div class="li"><a href="../sk/string/start.html" class="wikilink2" title="sk:string:start" rel="nofollow">sk</a></div></li> <li><div class="li"><a href="../tr/string/start.html" class="wikilink1" title="tr:string:start">tr</a></div></li> <li><div class="li"><a href="../tw/string/start.html" class="wikilink2" title="tw:string:start" rel="nofollow">tw</a></div></li></ul></div>
<div class="page">
<div class="googlead">
<script type="text/javascript"><!--
google_ad_client = "pub-6243170425937584";
google_ad_width = 120;
google_ad_height = 600;
google_ad_format = "120x600_as";
google_ad_type = "text";
google_ad_channel ="";
google_color_border = "EEEEFF";
google_color_bg = "FFFFFF";
google_color_link = "000000";
google_color_url = "336699";
google_color_text = "333333";
//--></script>
<script type="text/javascript"
</script>
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
<!-- wikipage start -->
<h1><a name="c_strings" id="c_strings">C++ Strings</a></h1>
<div class="level1">
<p>
The <code>basic_string</code> class provides a useful way to manipulate and store sequences of characters. It is defined as a basic template class in the <code>std</code> namespace in the <code><string></code> header file.
</p>
<pre class="c code c++" style="font-family:monospace;"> template <span class="sy0"><</span>typename Char<span class="sy0">,</span> typename Traits <span class="sy0">=</span> char_traits<span class="sy0"><</span>Char<span class="sy0">>,</span> typename Allocator <span class="sy0">=</span> allocator<span class="sy0"><</span>Char<span class="sy0">></span> <span class="sy0">></span>
class basic_string<span class="sy0">;</span></pre>
<p>
The C++ standard library provides two specializations of class <code>basic_string</code>.
</p>
<pre class="c code c++" style="font-family:monospace;"> <span class="kw4">typedef</span> basic_string<span class="sy0"><</span>char<span class="sy0">></span> <span class="kw4">string</span><span class="sy0">;</span>
<span class="kw4">typedef</span> basic_string<span class="sy0"><</span>wchar_t<span class="sy0">></span> wstring<span class="sy0">;</span></pre>
<p>
In the following sections, <code>string</code> means any specialization of class <code>basic_string</code>, and <code>Char</code> means its character type.
</p>
<table class="inline">
<tr class="row0">
<td class="col0"><a href="../string/string.html" class="wikilink1" title="string:string_constructors">Constructors</a></td><td class="col1">create strings from arrays of characters and other strings</td>
</tr>
<tr class="row1">
<td class="col0"><a href="../string/operator.html" class="wikilink1" title="string:string_operators">Operators</a></td><td class="col1">concatenate strings, assign strings, use strings for I/O, compare strings</td>
</tr>
<tr class="row2">
<td class="col0"><a href="../string/append.html" class="wikilink1" title="string:append">append</a></td><td class="col1">append characters and strings onto a string</td>
</tr>
<tr class="row3">
<td class="col0"><a href="../string/assign.html" class="wikilink1" title="string:assign">assign</a></td><td class="col1">give a string values from strings of characters and other C++ strings</td>
</tr>
<tr class="row4">
<td class="col0"><a href="../string/at.html" class="wikilink1" title="string:at">at</a></td><td class="col1">returns the character at a specific location</td>
</tr>
<tr class="row5">
<td class="col0"><a href="../string/begin.html" class="wikilink1" title="string:begin">begin</a></td><td class="col1">returns an iterator to the beginning of the string</td>
</tr>
<tr class="row6">
<td class="col0"><a href="../string/c_str.html" class="wikilink1" title="string:c_str">c_str</a></td><td class="col1">returns a non-modifiable standard C character array version of the string</td>
</tr>
<tr class="row7">
<td class="col0"><a href="../string/capacity.html" class="wikilink1" title="string:capacity">capacity</a></td><td class="col1">returns the number of characters that the string can hold</td>
</tr>
<tr class="row8">
<td class="col0"><a href="../string/clear.html" class="wikilink1" title="string:clear">clear</a></td><td class="col1">removes all characters from the string</td>
</tr>
<tr class="row9">
<td class="col0"><a href="../string/compare.html" class="wikilink1" title="string:compare">compare</a></td><td class="col1">compares two strings</td>
</tr>
<tr class="row10">
<td class="col0"><a href="../string/copy.html" class="wikilink1" title="string:copy">copy</a></td><td class="col1">copies characters from a string into an array</td>
</tr>
<tr class="row11">
<td class="col0"><a href="../string/data.html" class="wikilink1" title="string:data">data</a></td><td class="col1">returns a pointer to the first character of a string</td>
</tr>
<tr class="row12">
<td class="col0"><a href="../string/empty.html" class="wikilink1" title="string:empty">empty</a></td><td class="col1">true if the string has no characters</td>
</tr>
<tr class="row13">
<td class="col0"><a href="../string/end.html" class="wikilink1" title="string:end">end</a></td><td class="col1">returns an iterator just past the last character of a string</td>
</tr>
<tr class="row14">
<td class="col0"><a href="../string/erase.html" class="wikilink1" title="string:erase">erase</a></td><td class="col1">removes characters from a string</td>
</tr>
<tr class="row15">
<td class="col0"><a href="../string/find.html" class="wikilink1" title="string:find">find</a></td><td class="col1">find characters in the string</td>
</tr>
<tr class="row16">
<td class="col0"><a href="../string/find_first_not_of.html" class="wikilink1" title="string:find_first_not_of">find_first_not_of</a></td><td class="col1">find first absence of characters</td>
</tr>
<tr class="row17">
<td class="col0"><a href="../string/find_first_of.html" class="wikilink1" title="string:find_first_of">find_first_of</a></td><td class="col1">find first occurrence of characters</td>
</tr>
<tr class="row18">
<td class="col0"><a href="../string/find_last_not_of.html" class="wikilink1" title="string:find_last_not_of">find_last_not_of</a></td><td class="col1">find last absence of characters</td>
</tr>
<tr class="row19">
<td class="col0"><a href="../string/find_last_of.html" class="wikilink1" title="string:find_last_of">find_last_of</a></td><td class="col1">find last occurrence of characters</td>
</tr>
<tr class="row20">
<td class="col0"><a href="../string/getline.html" class="wikilink1" title="string:getline">getline</a></td><td class="col1">read data from an I/O stream into a string</td>
</tr>
<tr class="row21">
<td class="col0"><a href="../string/insert.html" class="wikilink1" title="string:insert">insert</a></td><td class="col1">insert characters into a string</td>
</tr>
<tr class="row22">
<td class="col0"><a href="../string/length.html" class="wikilink1" title="string:length">length</a></td><td class="col1">returns the length of the string</td>
</tr>
<tr class="row23">
<td class="col0"><a href="../string/max_size.html" class="wikilink1" title="string:max_size">max_size</a></td><td class="col1">returns the maximum number of characters that the string can hold</td>
</tr>
<tr class="row24">
<td class="col0"><a href="../string/npos.html" class="wikilink1" title="string:npos">npos</a></td><td class="col1">a special value that indicates “not found” or “all remaining characters”</td>
</tr>
<tr class="row25">
<td class="col0"><a href="../string/push_back.html" class="wikilink1" title="string:push_back">push_back</a></td><td class="col1">add a character to the end of the string</td>
</tr>
<tr class="row26">
<td class="col0"><a href="../string/rbegin.html" class="wikilink1" title="string:rbegin">rbegin</a></td><td class="col1">returns a reverse_iterator to the end of the string</td>
</tr>
<tr class="row27">
<td class="col0"><a href="../string/rend.html" class="wikilink1" title="string:rend">rend</a></td><td class="col1">returns a reverse_iterator to the beginning of the string</td>
</tr>
<tr class="row28">
<td class="col0"><a href="../string/replace.html" class="wikilink1" title="string:replace">replace</a></td><td class="col1">replace characters in the string</td>
</tr>
<tr class="row29">
<td class="col0"><a href="../string/reserve.html" class="wikilink1" title="string:reserve">reserve</a></td><td class="col1">sets the minimum capacity of the string</td>
</tr>
<tr class="row30">
<td class="col0"><a href="../string/resize.html" class="wikilink1" title="string:resize">resize</a></td><td class="col1">change the size of the string</td>
</tr>
<tr class="row31">
<td class="col0"><a href="../string/rfind.html" class="wikilink1" title="string:rfind">rfind</a></td><td class="col1">find the last occurrence of a substring</td>
</tr>
<tr class="row32">
<td class="col0"><a href="../string/size.html" class="wikilink1" title="string:size">size</a></td><td class="col1">returns the number of items in the string</td>
</tr>
<tr class="row33">
<td class="col0"><a href="../string/substr.html" class="wikilink1" title="string:substr">substr</a></td><td class="col1">returns a certain substring</td>
</tr>
<tr class="row34">
<td class="col0"><a href="../string/swap.html" class="wikilink1" title="string:swap">swap</a></td><td class="col1">swap the contents of this string with another</td>
</tr>
</table>
</div>
<!-- wikipage stop -->
</div>
<div class="clearer"> </div>
<div class="stylefoot">
<div class="meta">
<div class="user">
</div>
<!--
<div class="doc">
string/start.txt · Last modified: 10/15/2009 09:27 by 124.255.99.70 </div>
-->
</div>
<div class="bar" id="bar__bottom">
<div class="bar-left" id="bar__bottomleft">
<a href="../string/start.html" class="action edit" accesskey="e" rel="nofollow">Edit this page</a> •
<a href="../string/start.html" class="action revisions" accesskey="o" rel="nofollow">Old revisions</a> </div>
<div class="bar-right" id="bar__bottomright">
•
•
•
<a href="../string/start.html" class="action login" rel="nofollow">Login</a> •
<a href="../string/start.html" class="action index" accesskey="x" rel="nofollow">Index</a> •
<a href="../string/start.html" class="action recent" accesskey="r" rel="nofollow">Recent changes</a> •
<a href="../feed.php.html" title="Recent changes RSS feed">RSS</a> •
<form action="/wiki/" accept-charset="utf-8" class="search" id="dw__search"><div class="no"><input type="hidden" name="do" value="search" /><input type="text" id="qsearch__in" accesskey="f" name="id" class="edit" title="[ALT+F]" /><input type="submit" value="Search" class="button" title="Search" /><div id="qsearch__out" class="ajax_qsearch JSpopup"></div></div></form>
</div>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="no"><img src="/wiki/lib/exe/indexer.php?id=string%3Astart&1273193081" width="1" height="1" alt="" /></div>
</body>
</html>