Navigation
API > API/Runtime > API/Runtime/Core
A string view is a non-owning view of a range of characters.
Ensure that the underlying string is valid for the lifetime of the string view.
Be careful when constructing a string view from a temporary. Make a local copy if necessary.
FStringView View = Object->GetPathName(); // Invalid
FString PathName = Object->GetPathName(); // Valid FStringView View = PathName;
void ProcessPath(FStringView Path); // Valid ProcessPath(Object->GetPathName());
A string view is implicitly constructible from null-terminated strings, from contiguous ranges of characters such as FString and TStringBuilder, and from literals such as TEXTVIEW("...").
A string view is cheap to copy and is meant to be passed by value. Avoid passing by reference.
A string view is not guaranteed to represent a null-terminated string.
Log or format a string view using UE_LOG(TEXT("%.*s"), View.Len(), View.GetData());
A string view is a good fit for function parameters where the function has no requirements for how the string is stored. A caller may use FString, FStringView, TStringBuilder, a char array, a null-terminated string, or any other type which can convert to a string view.
The UE::String namespace contains many functions that can operate on string views. Most of these functions can be found in String/___.h in Core.
voidUseString(FStringViewInString);FStringString(TEXT("ABC"));constTCHAR*CString=*String;TStringBuilder<16>StringBuilder;StringBuilder.Append(TEXT("ABC"));UseString(String);UseString(CString);UseString(StringBuilder);UseString(TEXT("ABC"));UseString(TEXTVIEW("ABC"));
| Name | TStringView |
| Type | class |
| Header File | /Engine/Source/Runtime/Core/Public/Containers/StringView.h |
| Include Path | #include "Containers/StringView.h" |
Syntax
template<typename CharType>
class TStringView
Constructors
| Name | Remarks | Include Path | Unreal Specifiers |
|---|---|---|---|
constexpr TStringView
(
const CharRangeType& InRange |
Construct a view from a contiguous range of characters, such as FString or TStringBuilder. | Containers/StringView.h | |
TStringView
(
const OtherCharType* InData |
Construct a view of the null-terminated string pointed to by InData. | Containers/StringView.h | |
constexpr TStringView
(
const CharType* InData, |
Construct a view of InSize characters beginning at InData. | Containers/StringView.h | |
constexpr TStringView () |
Construct an empty view. | Containers/StringView.h | |
constexpr TStringView
(
const CharType* InData |
Construct a view of the null-terminated string pointed to by InData. | Containers/StringView.h | |
constexpr TStringView
(
const OtherCharType* InData, |
Construct a view of InSize characters beginning at InData. | Containers/StringView.h |
Typedefs
| Name | Type | Remarks | Include Path |
|---|---|---|---|
| ElementType | CharType | Containers/StringView.h | |
| ViewType | TStringView< CharType > | Containers/StringView.h |
Functions
Public
| Name | Remarks | Include Path | Unreal Specifiers |
|---|---|---|---|
const CharType * begin() |
DO NOT USE DIRECTLY STL-like iterators to enable range-based for loop support. | Containers/StringView.h | |
int32 Compare
(
const OtherCharType* Other, |
Compare this view with a null-terminated string. | Containers/StringView.h | |
int32 Compare
(
TStringView< OtherCharType > Other, |
Compare this view with a string view. | Containers/StringView.h | |
int32 Compare
(
OtherRangeType&& Other, |
Compare this view with a character range. | Containers/StringView.h | |
bool Contains
(
ViewType Search, |
Returns whether this view contains the specified substring. | Containers/StringView.h | |
int32 CopyString
(
CharType* Dest, |
Copy characters from the view into a destination buffer without null termination. | Containers/StringView.h | |
const CharType * end() |
Containers/StringView.h | ||
bool EndsWith
(
ViewType Suffix, |
Returns whether this view ends with the suffix with optional case sensitivity. | Containers/StringView.h | |
bool EndsWith
(
CharType Suffix |
Returns whether this view ends with the suffix character compared case-sensitively. | Containers/StringView.h | |
bool Equals
(
const OtherCharType* Other, |
Check whether this view is equivalent to a string view. | Containers/StringView.h | |
bool Equals
(
OtherRangeType&& Other, |
Check whether this view is equivalent to a character range. | Containers/StringView.h | |
int32 Find
(
ViewType Search, |
Search the view for the first occurrence of a search string. | Containers/StringView.h | |
bool FindChar
(
CharType Search, |
Search the view for the first occurrence of a character. | Containers/StringView.h | |
bool FindLastChar
(
CharType Search, |
Search the view for the last occurrence of a character. | Containers/StringView.h | |
const CharType * GetData() |
Returns a pointer to the start of the view. This is NOT guaranteed to be null-terminated! | Containers/StringView.h | |
bool IsEmpty() |
Returns whether the string view is empty. | Containers/StringView.h | |
bool IsValidIndex
(
int32 Index |
Tests if index is valid, i.e. greater than or equal to zero, and less than the number of characters in the string view. | Containers/StringView.h | |
ViewType Left
(
int32 CharCount |
Returns the left-most part of the view by taking the given number of characters from the left. | Containers/StringView.h | |
ViewType LeftChop
(
int32 CharCount |
Returns the left-most part of the view by chopping the given number of characters from the right. | Containers/StringView.h | |
void LeftChopInline
(
int32 CharCount |
Modifies the view by chopping the given number of characters from the right. | Containers/StringView.h | |
void LeftInline
(
int32 CharCount |
Modifies the view to be the given number of characters from the left. | Containers/StringView.h | |
int32 Len() |
Returns the length of the string view. | Containers/StringView.h | |
ViewType Mid
(
int32 Position, |
Returns the middle part of the view by taking up to the given number of characters from the given position. | Containers/StringView.h | |
void MidInline
(
int32 Position, |
Modifies the view to be the middle part by taking up to the given number of characters from the given position. | Containers/StringView.h | |
SIZE_T NumBytes() |
Containers/StringView.h | ||
TReversePointerIterator< const CharType > rbegin() |
Containers/StringView.h | ||
void RemovePrefix
(
int32 CharCount |
Modifies the view to remove the given number of characters from the start. | Containers/StringView.h | |
void RemoveSuffix
(
int32 CharCount |
Modifies the view to remove the given number of characters from the end. | Containers/StringView.h | |
TReversePointerIterator< const CharType > rend() |
Containers/StringView.h | ||
void Reset() |
Resets to an empty view | Containers/StringView.h | |
ViewType Right
(
int32 CharCount |
Returns the right-most part of the view by taking the given number of characters from the right. | Containers/StringView.h | |
ViewType RightChop
(
int32 CharCount |
Returns the right-most part of the view by chopping the given number of characters from the left. | Containers/StringView.h | |
void RightChopInline
(
int32 CharCount |
Modifies the view by chopping the given number of characters from the left. | Containers/StringView.h | |
void RightInline
(
int32 CharCount |
Modifies the view to be the given number of characters from the right. | Containers/StringView.h | |
bool StartsWith
(
CharType Prefix |
Returns whether this view starts with the prefix character compared case-sensitively. | Containers/StringView.h | |
bool StartsWith
(
ViewType Prefix, |
Returns whether this view starts with the prefix with optional case sensitivity. | Containers/StringView.h | |
ViewType SubStr
(
int32 Position, |
Alias for Mid. | Containers/StringView.h | |
ViewType TrimEnd() |
Returns the left part of the view before any whitespace at the end. | Containers/StringView.h | |
void TrimEndInline() |
Modifies the view to be the left part before any whitespace at the end. | Containers/StringView.h | |
ViewType TrimStart() |
Returns the right part of the view after any whitespace at the start. | Containers/StringView.h | |
ViewType TrimStartAndEnd() |
Returns the middle part of the view between any whitespace at the start and end. | Containers/StringView.h | |
void TrimStartAndEndInline() |
Modifies the view to be the middle part between any whitespace at the start and end. | Containers/StringView.h | |
void TrimStartInline() |
Modifies the view to be the right part after any whitespace at the start. | Containers/StringView.h |
Static
| Name | Remarks | Include Path | Unreal Specifiers |
|---|---|---|---|
static bool PrivateEquals
(
TStringView Lhs, |
Containers/StringView.h | ||
static bool PrivateEquals
(
TStringView Lhs, |
Containers/StringView.h | ||
static bool PrivateLess
(
TStringView Lhs, |
Containers/StringView.h |
Operators
| Name | Remarks | Include Path | Unreal Specifiers |
|---|---|---|---|
const CharType & operator[]
(
int32 Index |
Access the character at the given index in the view. | Containers/StringView.h |