TGUI  0.10-dev

Wrapper class to store strings. More...

#include <TGUI/String.hpp>

Public Member Functions

bool attemptToInt (int &result) const
 Converts the string to an integer. More...
 
bool attemptToUInt (unsigned int &result) const
 Converts the string to an unsigned int. More...
 
bool attemptToFloat (float &result) const
 Converts the string to a float. More...
 
int toInt (int defaultValue=0) const
 Converts the string to an integer. More...
 
unsigned int toUInt (unsigned int defaultValue=0) const
 Converts the string to an unsigned int. More...
 
float toFloat (float defaultValue=0) const
 Converts the string to a float. More...
 
String trim () const
 Returns a string with the whitespace at the start and end of this string removed. More...
 
String toLower () const
 Converts the ASCII characters in the string to lowercase. More...
 
String toUpper () const
 Converts the ASCII characters in the string to uppercase. More...
 
bool equalIgnoreCase (const String &other) const
 Compares this string to another and checks if they are equal if ASCII letters would have been lowercase. More...
 
bool startsWith (const String &substring) const
 Checks whether the first part of the string matches the given substring. More...
 
bool endsWith (const String &substring) const
 Checks whether the last part of the string matches the given substring. More...
 
Stringreplace (const String &searchFor, const String &replaceWith)
 Replaces all occurrences of a substring with a replacement string. More...
 
std::vector< Stringsplit (char32_t delimiter, bool trim=false) const
 Splits the string into multiple substrings given the delimiter that separates them. More...
 

Static Public Member Functions

static String join (const std::vector< String > &segments, const String &separator)
 Joins multiple string segments into a single string. More...
 
template<typename T >
static String fromNumber (T value)
 Construct the string from a number. More...
 
template<typename T >
static String fromNumberRounded (T value, int decimals)
 Construct the string from a floating point number, keeping only a certain amount of decimals behind the comma. More...
 

Detailed Description

Wrapper class to store strings.

A constructor and conversion operator are provided for sf::String to easily convert between sf::String and tgui::String.

The interface is similar to std::basic_string, but functions accept char, wchar_t, char8_t, char16_t and char32_t. Some extra helper functions are also provided to e.g. convert the string to an integer or float or to lowercase.

Note that when converting to std::string, an UTF-8 encoded string will be returned. Similarly, when passing an std::string or const char* to this class, the encoding is assumed to be UTF-8.

Data is stored in UTF-32, so any parameter or operator using a different encoding will have to convert the string internally and may be slightly slower than the variants that use UTF-32.

Member Function Documentation

◆ attemptToFloat()

bool tgui::String::attemptToFloat ( float &  result) const

Converts the string to a float.

Parameters
resultFloat value if the string contains a float. Unmodified if string is invalid.
Returns
Returns whether the string was valid and a value has been placed into the reference parameter.

◆ attemptToInt()

bool tgui::String::attemptToInt ( int &  result) const

Converts the string to an integer.

Parameters
resultInteger value if the string contains a base 10 integer. Unmodified if string is invalid.
Returns
Returns whether the string was valid and a value has been placed into the reference parameter.

◆ attemptToUInt()

bool tgui::String::attemptToUInt ( unsigned int &  result) const

Converts the string to an unsigned int.

Parameters
resultUnsigned integer value if the string contains a base 10 unsigned int. Unmodified if string is invalid.
Returns
Returns whether the string was valid and a value has been placed into the reference parameter.

◆ endsWith()

bool tgui::String::endsWith ( const String substring) const

Checks whether the last part of the string matches the given substring.

Parameters
substringCharacters to compare against the final part of the string
Returns
Does the back of the string match the given substring?

◆ equalIgnoreCase()

bool tgui::String::equalIgnoreCase ( const String other) const

Compares this string to another and checks if they are equal if ASCII letters would have been lowercase.

Parameters
otherThe other string to compare this one with
Returns
Are the strings equal except for the case of ASCII letters?

◆ fromNumber()

template<typename T >
static String tgui::String::fromNumber ( value)
inlinestatic

Construct the string from a number.

Parameters
valueNumber to convert to string
Returns
String representing given number

◆ fromNumberRounded()

template<typename T >
static String tgui::String::fromNumberRounded ( value,
int  decimals 
)
inlinestatic

Construct the string from a floating point number, keeping only a certain amount of decimals behind the comma.

Parameters
valueNumber to convert to string
decimalsDigits to keep behind the comma
Returns
String representing given number, rounded to the given decimals

◆ join()

static String tgui::String::join ( const std::vector< String > &  segments,
const String separator 
)
static

Joins multiple string segments into a single string.

Parameters
segmentsSubstrings that need to concatenated behind each other (with optional separators inbetween)
separatorCharacter that separates the substrings

◆ replace()

String & tgui::String::replace ( const String searchFor,
const String replaceWith 
)

Replaces all occurrences of a substring with a replacement string.

Parameters
searchForThe value being searched for
replaceWithThe value that replaces found searchFor values
Returns
Reference to this object.

◆ split()

std::vector< String > tgui::String::split ( char32_t  delimiter,
bool  trim = false 
) const

Splits the string into multiple substrings given the delimiter that separates them.

Parameters
delimiterCharacter that separates the substrings
trimShould whitespace be removed at the start and end of each part?

◆ startsWith()

bool tgui::String::startsWith ( const String substring) const

Checks whether the first part of the string matches the given substring.

Parameters
substringCharacters to compare against the first part of the string
Returns
Does the first part of the string match the given substring?

◆ toFloat()

float tgui::String::toFloat ( float  defaultValue = 0) const

Converts the string to a float.

Parameters
defaultValueValue to return if conversion fails
Returns
Returns the float value or defaultValue if the string didn't contain an float

◆ toInt()

int tgui::String::toInt ( int  defaultValue = 0) const

Converts the string to an integer.

Parameters
defaultValueValue to return if conversion fails
Returns
Returns the integer value or defaultValue if the string didn't contain a base 10 integer

◆ toLower()

String tgui::String::toLower ( ) const

Converts the ASCII characters in the string to lowercase.

Returns
Lowercase string

◆ toUInt()

unsigned int tgui::String::toUInt ( unsigned int  defaultValue = 0) const

Converts the string to an unsigned int.

Parameters
defaultValueValue to return if conversion fails
Returns
Returns the unsigned integer value or defaultValue if the string didn't contain a base 10 unsigned integer

◆ toUpper()

String tgui::String::toUpper ( ) const

Converts the ASCII characters in the string to uppercase.

Returns
Uppercase string

◆ trim()

String tgui::String::trim ( ) const

Returns a string with the whitespace at the start and end of this string removed.

Returns
Trimmed string

The documentation for this class was generated from the following file: