winrt::hstring struct (C++/WinRT)
A sequential collection of UTF-16 Unicode characters representing a text string. For more examples and info about winrt::hstring, see String handling in C++/WinRT.
The winrt::hstring type encapsulates HSTRING behind an interface similar to that of std::wstring. A HSTRING is a handle to a Windows Runtime string. For info about how to set an HSTRING into a winrt::hstring, and how to retrieve an HSTRING from a winrt::hstring, see Interoperating with the ABI's HSTRING.
struct hstring
Minimum supported SDK: Windows SDK version 10.0.17134.0 (Windows 10, version 1803)
Namespace: winrt
Header: %WindowsSdkDir%Include<WindowsTargetPlatformVersion>\cppwinrt\winrt\base.h (included by default)
Alias name | Type |
---|---|
hstring::value_type | A synonym for wchar_t. |
hstring::size_type | A synonym for uint32_t. |
hstring::const_reference | A synonym for hstring::value_type const&. |
hstring::const_pointer | A synonym for hstring::value_type const*. |
hstring::const_iterator | A synonym for hstring::const_pointer. |
hstring::const_reverse_iterator | A synonym for std::reverse_iterator<hstring::const_iterator>. |
Constructor | Description |
---|---|
hstring::hstring constructor | Initializes a new instance of the hstring struct with a copy of the input string data. |
Function | Description |
---|---|
hstring::back function | Returns a reference to the last character in the hstring object. |
hstring::begin function | Returns a const iterator to the first character in the hstring object. |
hstring::c_str function | Returns a pointer to the underlying null-terminated C-style string of the characters in the hstring object; no copy is made. |
hstring::cbegin function | Returns a const iterator to the first character in the hstring object. |
hstring::cend function | Returns a const iterator to one beyond the end of (one beyond the last character in) the hstring object. |
hstring::clear function | Makes the hstring object empty. |
hstring::crbegin function | Returns a const reverse iterator to one beyond the end of (one beyond the last character in) the hstring object. |
hstring::crend function | Returns a const reverse iterator to the first character in the hstring object. |
hstring::data function | Returns a null-terminated C-style string version of the characters in the hstring object. |
hstring::empty function | Returns a value indicating whether the hstring object is empty. |
hstring::end function | Returns a const iterator to one beyond the end of (one beyond the last character in) the hstring object. |
hstring::front function | Returns a reference to the first character in the hstring object. |
hstring::rbegin function | Returns a const reverse iterator to one beyond the end of (one beyond the last character in) the hstring object. |
hstring::rend function | Returns a const reverse iterator to the first character in the hstring object. |
hstring::size function | Returns the number of characters in the hstring object. |
Operator | Description |
---|---|
hstring::operator std::wstring_view | Converts the hstring object to a std::wstring_view. |
hstring::operator[] (subscript operator)) | Returns a reference to the character at the specified position within the hstring object. |
hstring::operator= (assignment operator) | Assigns a value to the hstring object. |
Function | Description |
---|---|
attach_abi function | Attaches a hstring object to a handle to a Windows Runtime string. |
copy_from_abi function | Copies to a hstring object from a handle to a Windows Runtime string. Clears the hstring, copies the parameter, and begins managing the handle. |
copy_to_abi function | Copies to a handle to a Windows Runtime string from a hstring object. |
detach_abi function | Detaches a hstring object from its handle, perhaps to return it to a caller. |
to_hstring function | Converts an input value to a winrt::hstring containing the value's string representation. |
Operator | Description |
---|---|
operator!= (inequality operator) | Returns a value indicating whether the two parameters are unequal to one another. |
operator+ (concatenation operator) | Returns a new hstring object resulting from concatenating the two parameters together. |
operator< (less-than operator) | Returns a value indicating whether the first parameter is less than the second parameter. |
operator<= (less-than-or-equal-to operator) | Returns a value indicating whether the first parameter is less than or equal to the second parameter. |
operator== (equality operator) | Returns a value indicating whether the two parameters are equal to one another. |
operator> (greater-than operator) | Returns a value indicating whether the first parameter is greater than the second parameter. |
operator>= (greater-than-or-equal-to operator) | Returns a value indicating whether the first parameter is greater than or equal to the second parameter. |
An hstring is a range, and that range is defined by the hstring::begin and hstring::end member functions, each of which returns a const iterator (as do hstring::cbegin and hstring::cend). Because of this, you can enumerate the characters in an hstring object with either a range-based for
statement, or with the std::for_each template function.
#include <iostream>
using namespace winrt;
...
void Iterators(hstring const& theHstring)
{
for (auto const& element : theHstring)
{
std::wcout << element;
}
std::for_each(theHstring.cbegin(), theHstring.cend(), [](T const& element) { std::wcout << element; });
}
Initializes a new instance of the hstring struct with a copy of the input string data.
hstring() noexcept;
hstring(winrt::hstring const& h);
explicit hstring(std::wstring_view const& v);
hstring(wchar_t const* c);
hstring(wchar_t const* c, uint32_t s);
h
An hstring value that initializes the hstring object.
v
A std::wstring_view value that initializes the hstring object.
c
A pointer to an array of constant wchar_t that initializes the hstring object.
s
A number that specifies a fixed size for the hstring object.
using namespace winrt;
...
void Constructors(
hstring const& theHstring,
std::wstring_view const& theWstringView,
wchar_t const* wideLiteral,
std::wstring const& wideString)
{
// hstring() noexcept
hstring fromDefault{};
// hstring(hstring const& h)
hstring fromHstring{ theHstring };
// explicit hstring(std::wstring_view const& value)
hstring fromWstringView{ theWstringView };
// hstring(wchar_t const* value)
hstring fromWideLiteral{ wideLiteral };
hstring fromWideString{ wideString.c_str() };
// hstring(wchar_t const* value, uint32_t size)
hstring fromWideLiteralWithSize{ wideLiteral, 256 };
hstring fromWideStringWithSize{ wideString.c_str(), 256 };
}
Returns a reference to the last character in the hstring object.
wchar_t const& back() const noexcept;
A reference to the last character in the hstring object.
Returns a const iterator to the first character in the hstring object. See Iterators.
wchar_t const* begin() const noexcept;
A const iterator to the first character in the hstring object.
Returns a pointer to the underlying null-terminated C-style string of the characters in the hstring object; no copy is made.
wchar_t const* c_str() const noexcept;
A pointer to the underlying null-terminated C-style string of the characters in the hstring object; no copy is made.
#include <iostream>
using namespace winrt;
...
void PrintHstring(hstring const& theHstring)
{
// You can get a standard wide string from an hstring.
std::wcout << theHstring.c_str() << std::endl;
}
Returns a const iterator to the first character in the hstring object. See Iterators.
wchar_t const* cbegin() const noexcept;
A const iterator to the first character in the hstring object.
Returns a const iterator to one beyond the end of (one beyond the last character in) the hstring object. See Iterators.
wchar_t const* cend() const noexcept;
A const iterator to one beyond the end of (one beyond the last character in) the hstring object.
Makes the hstring object empty.
void clear() noexcept;
Returns a const reverse iterator to one beyond the end of (one beyond the last character in) the hstring object.
std::reverse_iterator<wchar_t const*> crbegin() const noexcept;
A const reverse iterator to one beyond the end of (one beyond the last character in) the hstring object.
Returns a const reverse iterator to the first character in the hstring object.
std::reverse_iterator<wchar_t const*> crend() const noexcept;
A const reverse iterator to the first character in the hstring object.
Returns a null-terminated C-style string version of the characters in the hstring object.
wchar_t const* data() const noexcept;
A null-terminated C-style string version of the characters in the hstring object.
#include <iostream>
using namespace winrt;
...
void PrintHstring(hstring const& theHstring)
{
// You can get a standard wide string from an hstring.
std::wcout << theHstring.data() << std::endl;
}
Returns a value indicating whether the hstring object is empty.
bool empty() const noexcept;
true
if the hstring object is empty, otherwise false
.
Returns a const iterator to one beyond the end of (one beyond the last character in) the hstring object. See Iterators.
wchar_t const* end() const noexcept;
A const iterator to one beyond the end of (one beyond the last character in) the hstring object.
Returns a reference to the first character in the hstring object.
wchar_t const& front() const noexcept;
A reference to the first character in the hstring object.
Converts the hstring object to a std::wstring_view.
operator std::wstring_view() const noexcept;
The hstring object converted to a std::wstring_view.
using namespace winrt;
...
Uri contosoUri{ L"https://www.contoso.com" };
Uri awUri{ L"https://www.adventure-works.com" };
// Uri::Domain() is of type hstring. But we can use hstring's conversion operator to std::wstring_view.
std::wstring domainWstring{ contosoUri.Domain() }; // L"contoso.com"
domainWstring = awUri.Domain(); // L"https://www.adventure-works.com"
Returns a reference to the character at the specified position within the hstring object.
wchar_t const& operator[](uint32_t pos) const noexcept;
pos
A zero-based character position, or index.
A reference to the character at the specified position within the hstring object.
Assigns a value to the hstring object.
winrt::hstring& operator=(winrt::hstring const& h);
winrt::hstring& operator=(std::wstring_view const& v);
h
An hstring value to assign to the hstring object.
v
A std::wstring_view value to assign to the hstring object.
A reference to the hstring object.
Returns a const reverse iterator to one beyond the end of (one beyond the last character in) the hstring object.
std::reverse_iterator<wchar_t const*> rbegin() const noexcept;
A const reverse iterator to one beyond the end of (one beyond the last character in) the hstring object.
Returns a const reverse iterator to the first character in the hstring object.
std::reverse_iterator<wchar_t const*> rend() const noexcept;
A const reverse iterator to the first character in the hstring object.
Returns the number of characters in the hstring object.
uint32_t size() const noexcept;
A uint32_t containing the number of characters in the hstring object.
Attaches a hstring object to a handle to a Windows Runtime string.
void attach_abi(winrt::hstring& object, HSTRING value) noexcept;
object
A hstring object to operate on.
value
A handle to a Windows Runtime string.
Copies to a hstring object from a handle to a Windows Runtime string. Clears the hstring, copies the parameter, and begins managing the handle.
void copy_from_abi(winrt::hstring& object, HSTRING value);
object
A hstring object to operate on.
value
A handle to a Windows Runtime string.
Copies to a handle to a Windows Runtime string from a hstring object.
void copy_to_abi(winrt::hstring const& object, HSTRING& value);
object
A hstring object to operate on.
value
A handle reference, via which to copy the hstring's handle.
Detaches a hstring object from its handle, perhaps to return it to a caller.
HSTRING detach_abi(winrt::hstring& object) noexcept;
HSTRING detach_abi(winrt::hstring&& object) noexcept;
object
A hstring object to operate on.
The handle to the Windows Runtime string.
Returns a value indicating whether the two parameters are unequal to one another.
inline bool operator!=(winrt::hstring const& hLeft, winrt::hstring const& hRight) noexcept;
inline bool operator!=(winrt::hstring const& hLeft, std::wstring const& wRight) noexcept;
inline bool operator!=(winrt::hstring const& hLeft, wchar_t const* cRight) noexcept;
inline bool operator!=(std::wstring const& wLeft, winrt::hstring const& hRight) noexcept;
inline bool operator!=(wchar_t const* cLeft, winrt::hstring const& hRight) noexcept;
hLeft
hRight
An hstring value to compare with the other parameter.
wLeft
wRight
A std::wstring
value to compare with the other parameter.
cLeft
cRight
A pointer to an array of constant wchar_t to compare with the other parameter.
true
if the two parameters are unequal to one another, otherwise false
.
Returns a new hstring object resulting from concatenating the two parameters together.
inline hstring operator+(winrt::hstring const& hLeft, winrt::hstring const& hRight);
inline hstring operator+(winrt::hstring const& hLeft, std::wstring const& wRight);
inline hstring operator+(winrt::hstring const& hLeft, std::wstring_view const& vRight);
inline hstring operator+(winrt::hstring const& hLeft, wchar_t const* cRight);
inline hstring operator+(winrt::hstring const& hLeft, wchar_t scRight);
inline hstring operator+(std::wstring const& wLeft, winrt::hstring const& hRight);
inline hstring operator+(std::wstring_view const& vLeft, winrt::hstring const& hRight);
inline hstring operator+(wchar_t const* cLeft, winrt::hstring const& hRight);
inline hstring operator+(wchar_t scLeft, winrt::hstring const& hRight);
hLeft
hRight
An hstring value to concatenate with the other parameter.
wLeft
wRight
A std::wstring
value to concatenate with the other parameter.
vLeft
vRight
A std::wstring_view value to concatenate with the other parameter.
cLeft
cRight
A pointer to an array of constant wchar_t to concatenate with the other parameter.
scLeft
scRight
A wchar_t to concatenate with the other parameter.
A new hstring object resulting from concatenating the two parameters together.
Returns a value indicating whether the first parameter is less than the second parameter.
inline bool operator<(winrt::hstring const& hLeft, winrt::hstring const& hRight) noexcept;
inline bool operator<(winrt::hstring const& hLeft, std::wstring const& wRight) noexcept;
inline bool operator<(winrt::hstring const& hLeft, wchar_t const* cRight) noexcept;
inline bool operator<(std::wstring const& wLeft, winrt::hstring const& hRight) noexcept;
inline bool operator<(wchar_t const* cLeft, winrt::hstring const& hRight) noexcept;
hLeft
hRight
An hstring value to compare with the other parameter.
wLeft
wRight
A std::wstring
value to compare with the other parameter.
cLeft
cRight
A pointer to an array of constant wchar_t to compare with the other parameter.
true
if the first parameter is less than the second parameter, otherwise false
.
Returns a value indicating whether the first parameter is less than or equal to the second parameter.
inline bool operator<=(winrt::hstring const& hLeft, winrt::hstring const& hRight) noexcept;
inline bool operator<=(winrt::hstring const& hLeft, std::wstring const& wRight) noexcept;
inline bool operator<=(winrt::hstring const& hLeft, wchar_t const* cRight) noexcept;
inline bool operator<=(std::wstring const& wLeft, winrt::hstring const& hRight) noexcept;
inline bool operator<=(wchar_t const* cLeft, winrt::hstring const& hRight) noexcept;
hLeft
hRight
An hstring value to compare with the other parameter.
wLeft
wRight
A std::wstring
value to compare with the other parameter.
cLeft
cRight
A pointer to an array of constant wchar_t to compare with the other parameter.
true
if the first parameter is less than or equal to the second parameter, otherwise false
.
Returns a value indicating whether the two parameters are equal to one another.
inline bool operator==(winrt::hstring const& hLeft, winrt::hstring const& hRight) noexcept;
inline bool operator==(winrt::hstring const& hLeft, std::wstring const& wRight) noexcept;
inline bool operator==(winrt::hstring const& hLeft, wchar_t const* cRight) noexcept;
inline bool operator==(std::wstring const& wLeft, winrt::hstring const& hRight) noexcept;
inline bool operator==(wchar_t const* cLeft, winrt::hstring const& hRight) noexcept;
hLeft
hRight
An hstring value to compare with the other parameter.
wLeft
wRight
A std::wstring
value to compare with the other parameter.
cLeft
cRight
A pointer to an array of constant wchar_t to compare with the other parameter.
true
if the two parameters are equal to one another, otherwise false
.
Returns a value indicating whether the first parameter is greater than the second parameter.
inline bool operator>(winrt::hstring const& hLeft, winrt::hstring const& hRight) noexcept;
inline bool operator>(winrt::hstring const& hLeft, std::wstring const& wRight) noexcept;
inline bool operator>(winrt::hstring const& hLeft, wchar_t const* cRight) noexcept;
inline bool operator>(std::wstring const& wLeft, winrt::hstring const& hRight) noexcept;
inline bool operator>(wchar_t const* cLeft, winrt::hstring const& hRight) noexcept;
hLeft
hRight
An hstring value to compare with the other parameter.
wLeft
wRight
A std::wstring
value to compare with the other parameter.
cLeft
cRight
A pointer to an array of constant wchar_t to compare with the other parameter.
true
if the first parameter is greater than the second parameter, otherwise false
.
Returns a value indicating whether the first parameter is greater than or equal to the second parameter.
inline bool operator>=(winrt::hstring const& hLeft, winrt::hstring const& hRight) noexcept;
inline bool operator>=(winrt::hstring const& hLeft, std::wstring const& wRight) noexcept;
inline bool operator>=(winrt::hstring const& hLeft, wchar_t const* cRight) noexcept;
inline bool operator>=(std::wstring const& wLeft, winrt::hstring const& hRight) noexcept;
inline bool operator>=(wchar_t const* cLeft, winrt::hstring const& hRight) noexcept;
hLeft
hRight
An hstring value to compare with the other parameter.
wLeft
wRight
A std::wstring
value to compare with the other parameter.
cLeft
cRight
A pointer to an array of constant wchar_t to compare with the other parameter.
true
if the first parameter is greater than or equal to the second parameter, otherwise false
.