Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string"

From cppreference.com
< cpp
(add a few links to localization stuff, which does string conversions)
(starting on string_view)
Line 5: Line 5:
  
 
* {{lc|std::basic_string}} - a templated class designed to manipulate strings of any character type.
 
* {{lc|std::basic_string}} - a templated class designed to manipulate strings of any character type.
 +
* {{lc|std::basic_string_view}}{{mark c++17}} - a lightweight non-owning read-only view into a subsequence of a string.
 
* Null-terminated strings - arrays of characters terminated by a special ''null'' character.
 
* Null-terminated strings - arrays of characters terminated by a special ''null'' character.
  
Line 22: Line 23:
 
{{dsc end}}
 
{{dsc end}}
  
 +
{{rev begin}}
 +
{{rev|since=c++17|
 +
=== {{ltt|cpp/string/basic_string_view|std::basic_string_view}} ===
 +
 +
The templated class {{lc|std::basic_string_view}} provides a lightweight object that offers read-only access to a string or a part of a string using an interface similar to the interface of {{lc|std::basic_string}}.
 +
 +
Several specializations of {{lc|std::basic_string_view}} are provided for commonly-used types:
 +
 +
{{dsc begin}}
 +
{{dsc header | string}}
 +
{{dsc hitem | Type | Definition}}
 +
{{dsc | {{lc|std::string_view}} | {{c|std::basic_string_view<char>}} | notes={{mark since c++17}}}}
 +
{{dsc | {{lc|std::wstring_view}} | {{c|std::basic_string_view<wchar_t>}} | notes={{mark since c++17}}}}
 +
{{dsc | {{lc|std::u16string_view}} | {{c|std::basic_string_view<char16_t>}} | notes={{mark since c++17}}}}
 +
{{dsc | {{lc|std::u32string_view}} | {{c|std::basic_string_view<char32_t>}} | notes={{mark since c++17}}}}
 +
{{dsc end}}
 +
}}
 +
{{rev end}}
 
=== Null-terminated strings ===
 
=== Null-terminated strings ===
  

Revision as of 14:45, 17 March 2016

The C++ strings library includes support for two general types of strings:

  • std::basic_string - a templated class designed to manipulate strings of any character type.
  • std::basic_string_view(C++17) - a lightweight non-owning read-only view into a subsequence of a string.
  • Null-terminated strings - arrays of characters terminated by a special null character.

Contents

std::basic_string

The templated class std::basic_string generalizes how sequences of characters are manipulated and stored. String creation, manipulation, and destruction are all handled by a convenient set of class methods and related functions.

Several specializations of std::basic_string are provided for commonly-used types:

Defined in header <string>
Type Definition
std::string std::basic_string<char>
std::wstring std::basic_string<wchar_t>
std::u16string std::basic_string<char16_t>
std::u32string std::basic_string<char32_t>

std::basic_string_view

The templated class std::basic_string_view provides a lightweight object that offers read-only access to a string or a part of a string using an interface similar to the interface of std::basic_string.

Several specializations of std::basic_string_view are provided for commonly-used types:

Defined in header <string>
Type Definition
std::string_view std::basic_string_view<char>
std::wstring_view std::basic_string_view<wchar_t>
std::u16string_view std::basic_string_view<char16_t>
std::u32string_view std::basic_string_view<char32_t>
(since C++17)

Null-terminated strings

Null-terminated strings are arrays of characters that are terminated by a special null character. C++ provides functions to create, inspect, and modify null-terminated strings.

There are three types of null-terminated strings:

Additional support

std::char_traits

The string library also provides class template std::char_traits that defines types and functions for std::basic_string. The following specializations are defined:

Defined in header <string>
template<> class char_traits<char>;

template<> class char_traits<wchar_t>;
template<> class char_traits<char16_t>;

template<> class char_traits<char32_t>;


(since C++11)
(since C++11)

Conversions and classification

The localizations library provides support for string conversions (e.g. std::wstring_convert or std::toupper) as well as functions that classify characters (e.g. std::isspace or std::isdigit).

See also

C++ documentation for Localizations library
C documentation for Strings library