Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string"

From cppreference.com
< cpp
m (Undo revision 142517 by Space Mission (talk))
(c -> c/core.)
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.
+
* {{lc|std::basic_string_view}} {{mark since 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.
  
=== {{lc|std::basic_string}} ===
+
==={{lc|std::basic_string}}===
 
+
 
The templated class {{lc|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.
 
The templated class {{lc|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 {{lc|std::basic_string}} are provided for commonly-used types:
 
Several specializations of {{lc|std::basic_string}} are provided for commonly-used types:
 
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc header | string}}
+
{{dsc header|string}}
{{dsc hitem | Type | Definition}}
+
{{dsc hitem|Type|Definition}}
{{dsc | {{lc|std::string}} | {{c|std::basic_string<char>}}}}
+
{{dsc|{{ttb|std::string}}|{{c/core|std::basic_string<char>}}}}
{{dsc | {{lc|std::wstring}} | {{c|std::basic_string<wchar_t>}}}}
+
{{dsc|{{ttb|std::wstring}}|{{c/core|std::basic_string<wchar_t>}}}}
{{dsc | {{lc|std::u8string}} {{mark since c++20}} | {{c|std::basic_string<char8_t>}}}}
+
{{dsc|{{ttb|std::u8string}} {{mark since c++20}}|{{c/core|std::basic_string<char8_t>}}}}
{{dsc | {{lc|std::u16string}} {{mark since c++11}} | {{c|std::basic_string<char16_t>}}}}
+
{{dsc|{{ttb|std::u16string}} {{mark since c++11}}|{{c/core|std::basic_string<char16_t>}}}}
{{dsc | {{lc|std::u32string}} {{mark since c++11}} | {{c|std::basic_string<char32_t>}}}}
+
{{dsc|{{ttb|std::u32string}} {{mark since c++11}}|{{c/core|std::basic_string<char32_t>}}}}
 
{{dsc end}}
 
{{dsc end}}
 +
  
 
{{rrev|since=c++17|
 
{{rrev|since=c++17|
=== {{ltt|cpp/string/basic_string_view|std::basic_string_view}} ===
+
==={{lc|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}}.
 
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:
 
Several specializations of {{lc|std::basic_string_view}} are provided for commonly-used types:
 
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc header | string_view}}
+
{{dsc header|string_view}}
{{dsc hitem | Type | Definition}}
+
{{dsc hitem|Type|Definition}}
{{dsc | {{lc|std::string_view}} {{mark since c++17}} | {{c|std::basic_string_view<char>}}}}
+
{{dsc|{{ttb|std::string_view}}|{{c/core|std::basic_string_view<char>}}}}
{{dsc | {{lc|std::wstring_view}} {{mark since c++17}} | {{c|std::basic_string_view<wchar_t>}}}}
+
{{dsc|{{ttb|std::wstring_view}}|{{c/core|std::basic_string_view<wchar_t>}}}}
{{dsc | {{lc|std::u8string_view}} {{mark since c++20}} | {{c|std::basic_string_view<char8_t>}}}}
+
{{dsc|{{ttb|std::u8string_view}} {{mark since c++20}}|{{c/core|std::basic_string_view<char8_t>}}}}
{{dsc | {{lc|std::u16string_view}} {{mark since c++17}} | {{c|std::basic_string_view<char16_t>}}}}
+
{{dsc|{{ttb|std::u16string_view}}|{{c/core|std::basic_string_view<char16_t>}}}}
{{dsc | {{lc|std::u32string_view}} {{mark since c++17}} | {{c|std::basic_string_view<char32_t>}}}}
+
{{dsc|{{ttb|std::u32string_view}}|{{c/core|std::basic_string_view<char32_t>}}}}
 
{{dsc end}}
 
{{dsc end}}
 
}}
 
}}
=== 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.
+
===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:
 
There are three types of null-terminated strings:
 +
* {{rl|byte|null-terminated byte strings}}
 +
* {{rl|multibyte|null-terminated multibyte strings}}
 +
* {{rl|wide|null-terminated wide strings}}
  
* {{rl | byte | null-terminated byte strings}}
+
===Additional support===
* {{rl | multibyte | null-terminated multibyte strings}}
+
* {{rl | wide | null-terminated wide strings}}
+
 
+
=== Additional support ===
+
 
+
 
===={{lc|std::char_traits}}====
 
===={{lc|std::char_traits}}====
 +
The string library also provides class template {{lc|std::char_traits}} that defines types and functions for {{lc|std::basic_string}}{{rev inl|since=c++17| and {{lc|std::basic_string_view}}}}.
  
The string library also provides class template {{lc|std::char_traits}} that defines types and functions for {{lc|std::basic_string}} {{rev inl|since=c++17|and {{lc|std::basic_string_view}}}}. The following specializations are defined:
+
The following specializations are defined:
 
+
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | string}}
+
{{dcl header|string}}
{{dcl | template<> class char_traits<char>; }}
+
{{dcl|template<> class char_traits<char>;}}
{{dcl | template<> class char_traits<wchar_t>; }}
+
{{dcl|template<> class char_traits<wchar_t>;}}
{{dcl | since=c++20 | template<> class char_traits<char8_t>; }}
+
{{dcl|template<> class char_traits<char8_t>;|since=c++20}}
{{dcl | since=c++11 | template<> class char_traits<char16_t>; }}
+
{{dcl|template<> class char_traits<char16_t>;|since=c++11}}
{{dcl | since=c++11 | template<> class char_traits<char32_t>; }}
+
{{dcl|template<> class char_traits<char32_t>;|since=c++11}}
 
{{dcl end}}
 
{{dcl end}}
  
 
====Conversions and classification====
 
====Conversions and classification====
 
+
The [[cpp/locale|localizations library]] provides support for string conversions (e.g. {{lc|std::wstring_convert}} or {{ltt std|cpp/locale/toupper}}) as well as functions that classify characters (e.g. {{ltt std|cpp/locale/isspace}} or {{ltt std|cpp/locale/isdigit}}).
The [[cpp/locale|localizations library]] provides support for string conversions (e.g. {{lc|std::wstring_convert}} or {{lc|std::toupper}}) as well as functions that classify characters (e.g. {{lc|std::isspace}} or {{lc|std::isdigit}}).
+
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc see cpp | cpp/locale | Localizations library | nomono=true}}
+
{{dsc see cpp|cpp/locale|Localizations library|nomono=true}}
{{dsc see c | c/string | Strings library | nomono=true}}
+
{{dsc see c|c/string|Strings library|nomono=true}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|ar|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|ar|de|es|fr|it|ja|pt|ru|zh}}

Revision as of 17:49, 27 July 2023

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

  • std::basic_string - a templated class designed to manipulate strings of any character type.
  • std::basic_string_view (since 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::u8string (since C++20) std::basic_string<char8_t>
std::u16string (since C++11) std::basic_string<char16_t>
std::u32string (since C++11) 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_view>
Type Definition
std::string_view std::basic_string_view<char>
std::wstring_view std::basic_string_view<wchar_t>
std::u8string_view (since C++20) std::basic_string_view<char8_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 and std::basic_string_view(since C++17).

The following specializations are defined:

Defined in header <string>
template<> class char_traits<char>;
template<> class char_traits<wchar_t>;
template<> class char_traits<char8_t>;
(since C++20)
template<> class char_traits<char16_t>;
(since C++11)
template<> class char_traits<char32_t>;
(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