Template Numerical Library version\ main:94209208
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
TNL::String Class Reference

Class for managing strings. More...

#include <TNL/String.h>

Inheritance diagram for TNL::String:
Inheritance graph
[legend]
Collaboration diagram for TNL::String:
Collaboration graph
[legend]

Public Types

enum class  SplitSkip { NoSkip , SkipEmpty }
 This enum defines how the operation split of string is to be performed. More...
 

Public Member Functions

 String ()=default
 Default constructor.
 
 String (const std::string &str)
 Initialization by std::string.
 
 String (const String &)=default
 Default copy constructor.
 
 String (String &&)=default
 Default move constructor.
 
bool endsWith (const String &suffix) const
 Checks if the string ends with given suffix.
 
int getAllocatedSize () const
 Returns size of allocated storage for given string.
 
char * getData ()
 Returns pointer to data. Alias of std::string::data.
 
const char * getData () const
 Returns pointer to data. Alias of std::string::data.
 
int getLength () const
 Returns the number of characters in given string. Equivalent to getSize.
 
int getSize () const
 Returns the number of characters in given string.
 
const char * getString () const
 Returns pointer to data.
 
 operator bool () const
 Cast to bool operator.
 
bool operator! () const
 Cast to bool with negation operator.
 
bool operator!= (char str) const
 This function overloads operator!=.
 
bool operator!= (const char *str) const
 This function overloads operator!=.
 
bool operator!= (const std::string &str) const
 This function overloads operator!=.
 
bool operator!= (const String &str) const
 This function overloads operator!=.
 
String operator+ (char str) const
 This function concatenates strings and returns a newly constructed string object.
 
String operator+ (const char *str) const
 This function concatenates C strings str and returns a newly constructed string object.
 
String operator+ (const std::string &str) const
 This function concatenates C strings str and returns a newly constructed string object.
 
String operator+ (const String &str) const
 This function concatenates C strings str and returns a newly constructed string object.
 
Stringoperator+= (char str)
 This function overloads operator+=.
 
Stringoperator+= (const char *str)
 This function overloads operator+=.
 
Stringoperator+= (const std::string &str)
 This function overloads operator+=.
 
Stringoperator+= (const String &str)
 This function overloads operator+=.
 
Stringoperator= (const String &)=default
 Default copy assignment operator.
 
Stringoperator= (String &&)=default
 Default move assignment operator.
 
bool operator== (char str) const
 This function checks whether the given string is equal to str.
 
bool operator== (const char *str) const
 This function overloads operator==.
 
bool operator== (const std::string &str) const
 This function overloads operator==.
 
bool operator== (const String &str) const
 This function overloads operator==.
 
char & operator[] (int i)
 Operator for accessing particular chars of the string.
 
const char & operator[] (int i) const
 Operator for accessing particular chars of the string.
 
String replace (const String &pattern, const String &replaceWith, int count=0) const
 This method replaces part of the string.
 
void setSize (int size)
 Reserves space for given size.
 
std::vector< Stringsplit (char separator=' ', SplitSkip skipEmpty=SplitSkip::NoSkip) const
 Splits string into list of strings with respect to given separator.
 
bool startsWith (const String &prefix) const
 Checks if the string starts with given prefix.
 
String strip (char strip=' ') const
 Trims/strips this string.
 
- Public Member Functions inherited from std::string
string (T... args)
 
append (T... args)
 
assign (T... args)
 
at (T... args)
 
back (T... args)
 
begin (T... args)
 
c_str (T... args)
 
capacity (T... args)
 
cbegin (T... args)
 
cend (T... args)
 
clear (T... args)
 
compare (T... args)
 
copy (T... args)
 
crbegin (T... args)
 
crend (T... args)
 
data (T... args)
 
empty (T... args)
 
end (T... args)
 
erase (T... args)
 
find (T... args)
 
find_first_not_of (T... args)
 
find_first_of (T... args)
 
find_last_not_of (T... args)
 
find_last_of (T... args)
 
front (T... args)
 
get_allocator (T... args)
 
insert (T... args)
 
length (T... args)
 
max_size (T... args)
 
operator= (T... args)
 
operator[] (T... args)
 
pop_back (T... args)
 
push_back (T... args)
 
rbegin (T... args)
 
rend (T... args)
 
replace (T... args)
 
reserve (T... args)
 
resize (T... args)
 
rfind (T... args)
 
shrink_to_fit (T... args)
 
size (T... args)
 
substr (T... args)
 
swap (T... args)
 
- Public Member Functions inherited from std::basic_string< Char >
basic_string (T... args)
 
append (T... args)
 
assign (T... args)
 
at (T... args)
 
back (T... args)
 
begin (T... args)
 
c_str (T... args)
 
capacity (T... args)
 
cbegin (T... args)
 
cend (T... args)
 
clear (T... args)
 
compare (T... args)
 
copy (T... args)
 
crbegin (T... args)
 
crend (T... args)
 
data (T... args)
 
empty (T... args)
 
end (T... args)
 
erase (T... args)
 
find (T... args)
 
find_first_not_of (T... args)
 
find_first_of (T... args)
 
find_last_not_of (T... args)
 
find_last_of (T... args)
 
front (T... args)
 
get_allocator (T... args)
 
insert (T... args)
 
length (T... args)
 
max_size (T... args)
 
operator= (T... args)
 
operator[] (T... args)
 
pop_back (T... args)
 
push_back (T... args)
 
rbegin (T... args)
 
rend (T... args)
 
replace (T... args)
 
reserve (T... args)
 
resize (T... args)
 
rfind (T... args)
 
shrink_to_fit (T... args)
 
size (T... args)
 
substr (T... args)
 
swap (T... args)
 

Detailed Description

Class for managing strings.

The following example shows common use of String.

Example
#include <iostream>
#include <TNL/String.h>
#include <TNL/File.h>
using namespace TNL;
int
main( int argc, char* argv[] )
{
String emptyString;
String string1( "string 1" );
String string2( "string 2" );
String string3( string2 );
String string4 = convertToString( 28.4 );
std::cout << "empytString = " << emptyString << std::endl;
std::cout << "string1 = " << string1 << std::endl;
std::cout << "string2 = " << string2 << std::endl;
std::cout << "string3 = " << string3 << std::endl;
std::cout << "string4 = " << string4 << std::endl;
std::cout << "emptyString size = " << emptyString.getSize() << std::endl;
std::cout << "string1 size = " << string1.getSize() << std::endl;
std::cout << "string1 length = " << string1.getLength() << std::endl;
const char* c_string = string1.getString();
std::cout << "c_string = " << c_string << std::endl;
std::cout << " 3rd letter of string1 =" << string1[ 2 ] << std::endl;
std::cout << " string1 + string2 = " << string1 + string2 << std::endl;
std::cout << " string1 + \" another string\" = " << string1 + " another string" << std::endl;
string2 += "another string";
std::cout << " string2 = " << string2;
string2 = "string 2";
if( string3 == string2 )
std::cout << "string3 == string2" << std::endl;
if( string1 != string2 )
std::cout << "string1 != string2" << std::endl;
if( ! emptyString )
std::cout << "emptyString is empty" << std::endl;
if( string1 )
std::cout << "string1 is not empty" << std::endl;
File myFile;
myFile.open( "string_save.out", std::ios_base::out );
myFile << string1;
myFile.close();
myFile.open( "string_save.out", std::ios_base::in );
myFile >> string3;
std::cout << "string 3 after loading = " << string3 << std::endl;
}
This class serves for binary IO. It allows to do IO even for data allocated on GPU together with on-t...
Definition File.h:25
void close()
Closes the file.
Definition File.hpp:51
void open(const std::string &fileName, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
Open given file.
Definition File.hpp:25
Class for managing strings.
Definition String.h:30
int getSize() const
Returns the number of characters in given string.
Definition String.hpp:19
T endl(T... args)
The main TNL namespace.
Definition AtomicOperations.h:9
String convertToString(const T &value)
Converts value of type T to a String.
Definition String.h:406
Output
empytString =
string1 = string 1
string2 = string 2
string3 = string 2
string4 = 28.4
emptyString size = 0
string1 size = 8
string1 length = 8
c_string = string 1
3rd letter of string1 =r
string1 + string2 = string 1string 2
string1 + " another string" = string 1 another string
string2 = string 2another stringstring3 == string2
string1 != string2
emptyString is empty
string1 is not empty
string 3 after loading = string 1

In addition to methods of this class, check the following related functions:

convertToString

operator+

Member Enumeration Documentation

◆ SplitSkip

enum class TNL::String::SplitSkip
strong

This enum defines how the operation split of string is to be performed.

Enumerator
NoSkip 

Do not skip empty characters.

SkipEmpty 

Skip empty characters.

Constructor & Destructor Documentation

◆ String()

TNL::String::String ( )
default

Default constructor.

Constructs an empty string object.

Member Function Documentation

◆ getAllocatedSize()

int TNL::String::getAllocatedSize ( ) const
inline

Returns size of allocated storage for given string.

Example
#include <iostream>
#include <TNL/String.h>
using namespace TNL;
int
main()
{
String str( "my world" );
std::cout << "Allocated_size = " << str.getAllocatedSize() << std::endl;
}
Output
Allocated_size = 15

◆ getString()

const char * TNL::String::getString ( ) const
inline

Returns pointer to data.

It returns the content of the given string as a constant pointer to char.

◆ operator bool()

TNL::String::operator bool ( ) const
inline

Cast to bool operator.

This operator converts string to boolean expression (true or false). It returns true if the string is NOT empty. Otherwise it returns false.

◆ operator!()

bool TNL::String::operator! ( ) const
inline

Cast to bool with negation operator.

This operator converts string to boolean expression (false or true). It returns true if the string is empty. Otherwise it returns false.

◆ operator!=() [1/4]

bool TNL::String::operator!= ( char str) const
inline

This function overloads operator!=.

It returns true when the given string is NOT equal to str. Otherwise it returns true.

◆ operator!=() [2/4]

bool TNL::String::operator!= ( const char * str) const
inline

This function overloads operator!=.

It returns true when the given string is NOT equal to str. Otherwise it returns true.

◆ operator!=() [3/4]

bool TNL::String::operator!= ( const std::string & str) const
inline

This function overloads operator!=.

It returns true when the given string is NOT equal to str. Otherwise it returns true.

◆ operator!=() [4/4]

bool TNL::String::operator!= ( const String & str) const
inline

This function overloads operator!=.

It returns true when the given string is NOT equal to str. Otherwise it returns true.

◆ operator+=() [1/4]

String & TNL::String::operator+= ( char str)
inline

This function overloads operator+=.

Operators for single characters.

Appends character str to this string.

◆ operator+=() [2/4]

String & TNL::String::operator+= ( const char * str)
inline

This function overloads operator+=.

Operators for C strings.

It appends the C string str to this string.

◆ operator+=() [3/4]

String & TNL::String::operator+= ( const std::string & str)
inline

This function overloads operator+=.

Operators for std::string.

It appends the C string str to this string.

◆ operator+=() [4/4]

String & TNL::String::operator+= ( const String & str)
inline

This function overloads operator+=.

Operators for String.

It appends the C string str to this string.

◆ operator==() [1/4]

bool TNL::String::operator== ( char str) const
inline

This function checks whether the given string is equal to str.

It returns true when the given string is equal to str. Otherwise it returns false.

◆ operator==() [2/4]

bool TNL::String::operator== ( const char * str) const
inline

This function overloads operator==.

It returns true when the given string is equal to str. Otherwise it returns false.

◆ operator==() [3/4]

bool TNL::String::operator== ( const std::string & str) const
inline

This function overloads operator==.

It returns true when the given string is equal to str. Otherwise it returns false.

◆ operator==() [4/4]

bool TNL::String::operator== ( const String & str) const
inline

This function overloads operator==.

It returns true when the given string is equal to str. Otherwise it returns false.

◆ operator[]() [1/2]

char & TNL::String::operator[] ( int i)
inline

Operator for accessing particular chars of the string.

It returns the character at the position i in given string as a modifiable reference.

◆ operator[]() [2/2]

const char & TNL::String::operator[] ( int i) const
inline

Operator for accessing particular chars of the string.

This function overloads operator[]. It returns a reference to the character at position i in given string. The character can not be changed be user.

◆ replace()

String TNL::String::replace ( const String & pattern,
const String & replaceWith,
int count = 0 ) const
inline

This method replaces part of the string.

It replaces pattern in this string with a string replaceWith. If parameter count is defined, the function makes replacement only count occurrences, of the given pattern. If count is zero, all pattern occurrences are replaced.

Parameters
patternto be replaced.
replaceWithstring the pattern will be replaced with.
countnumber of occurrences to be replaced. All occurrences are replaced if count is zero..
Example
#include <iostream>
#include <TNL/String.h>
using namespace TNL;
int
main()
{
String phrase( "Say yes yes yes!" );
std::cout << "phrase.replace( \"yes\", \"no\", 1 ) = " << phrase.replace( "yes", "no", 1 ) << std::endl;
std::cout << "phrase.replace( \"yes\", \"no\", 2 ) = " << phrase.replace( "yes", "no", 2 ) << std::endl;
std::cout << "phrase.replace( \"yes\", \"no\", 3 ) = " << phrase.replace( "yes", "no", 3 ) << std::endl;
}
Output
phrase.replace( "yes", "no", 1 ) = Say no yes yes!
phrase.replace( "yes", "no", 2 ) = Say no no yes!
phrase.replace( "yes", "no", 3 ) = Say no no no!

◆ setSize()

void TNL::String::setSize ( int size)
inline

Reserves space for given size.

Requests to allocate storage space of given size to avoid memory reallocation. It allocates one more byte for the terminating 0.

Parameters
sizeNumber of characters.
Example
#include <iostream>
#include <TNL/String.h>
using namespace TNL;
int
main()
{
String string;
string.setSize( 1024 );
std::cout << "String size = " << string.getSize() << std::endl;
std::cout << "Allocated size = " << string.getAllocatedSize() << std::endl;
}
void setSize(int size)
Reserves space for given size.
Definition String.hpp:31
Output
String size = 1024
Allocated size = 1024

◆ split()

std::vector< String > TNL::String::split ( char separator = ' ',
SplitSkip skipEmpty = SplitSkip::NoSkip ) const
inline

Splits string into list of strings with respect to given separator.

This method splits the string into sequence of substrings divided by occurrences of separator. It returns the list of those strings via std::vector. When separator does not appear anywhere in the given string, this function returns a single-element list containing given sting. If skipEmpty equals SkipEmpty no empty substrings are inserted into the resulting container.

Parameters
separatoris a character separating substrings in given string.
skipEmpty
Example
#include <iostream>
#include <TNL/String.h>
using namespace TNL;
int
main()
{
String dates( "3/4/2005;8/7/2011;11/12/2019" );
std::vector< String > list = dates.split( ';' );
std::cout << "list_dates = " << list[ 0 ] << ", " << list[ 1 ] << ", " << list[ 2 ] << std::endl;
String cars( "Subaru,Mazda,,Skoda," );
std::vector< String > list3 = cars.split( ',', String::SplitSkip::SkipEmpty );
std::cout << "split with String::SkipEmpty = " << list3[ 0 ] << ", " << list3[ 1 ] << ", " << list3[ 2 ] << std::endl;
std::vector< String > list5 = cars.split( ',' );
std::cout << "split without String::SkipEmpty = " << list5[ 0 ] << ", " << list5[ 1 ] << ", " << list5[ 2 ] << ", "
<< list5[ 3 ] << std::endl;
}
Output
list_dates = 3/4/2005, 8/7/2011, 11/12/2019
split with String::SkipEmpty = Subaru, Mazda, Skoda
split without String::SkipEmpty = Subaru, Mazda, , Skoda

◆ strip()

String TNL::String::strip ( char strip = ' ') const
inline

Trims/strips this string.

Removes all 'spaces' from given string except for single 'spaces' between words.

Parameters
stripcan be used to change the character to be removed.
Example
#include <iostream>
#include <TNL/String.h>
using namespace TNL;
int
main()
{
String names( " Josh Martin John Marley Charles " );
String names2( ".......Josh Martin...John..Marley.Charles..." );
std::cout << "names strip is: " << names.strip() << std::endl;
std::cout << "names2 strip is: " << names.strip( '.' ) << std::endl;
}
Output
names strip is: Josh Martin John Marley Charles
names2 strip is: Josh Martin John Marley Charles

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