Template Numerical Library: TNL::String Class Reference

Template Numerical Library version\ main:16b9213d5

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:
[legend]
Collaboration diagram for TNL::String:
[legend]

## Public Types
enum class | SplitSkip : std::uint8_t { 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.
String & | operator+= (char str)
| This function overloads operator+=.
String & | operator+= (const char *str)
| This function overloads operator+=.
String & | operator+= (const std::string &str)
| This function overloads operator+=.
String & | operator+= (const String &str)
| This function overloads operator+=.
String & | operator= (const String &)=default
| Default copy assignment operator.
String & | operator= (String &&)=default
| Default move assignment operator.
T | operator= (T... args)
| Inherited assignment operators.
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< String > | split (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.
T | string (T... args)
| Inherited constructors.
String | strip (char strip=' ') const
| Trims/strips this string.
Public Member Functions inherited from std::string
T | string (T... args)
T | append (T... args)
T | assign (T... args)
T | at (T... args)
T | back (T... args)
T | begin (T... args)
T | c_str (T... args)
T | capacity (T... args)
T | cbegin (T... args)
T | cend (T... args)
T | clear (T... args)
T | compare (T... args)
T | copy (T... args)
T | crbegin (T... args)
T | crend (T... args)
T | data (T... args)
T | empty (T... args)
T | end (T... args)
T | erase (T... args)
T | find (T... args)
T | find_first_not_of (T... args)
T | find_first_of (T... args)
T | find_last_not_of (T... args)
T | find_last_of (T... args)
T | front (T... args)
T | get_allocator (T... args)
T | insert (T... args)
T | length (T... args)
T | max_size (T... args)
T | operator= (T... args)
T | operator[] (T... args)
T | pop_back (T... args)
T | push_back (T... args)
T | rbegin (T... args)
T | rend (T... args)
T | replace (T... args)
T | reserve (T... args)
T | resize (T... args)
T | rfind (T... args)
T | shrink_to_fit (T... args)
T | size (T... args)
T | substr (T... args)
T | swap (T... args)
Public Member Functions inherited from std::basic_string< Char >
T | basic_string (T... args)
T | append (T... args)
T | assign (T... args)
T | at (T... args)
T | back (T... args)
T | begin (T... args)
T | c_str (T... args)
T | capacity (T... args)
T | cbegin (T... args)
T | cend (T... args)
T | clear (T... args)
T | compare (T... args)
T | copy (T... args)
T | crbegin (T... args)
T | crend (T... args)
T | data (T... args)
T | empty (T... args)
T | end (T... args)
T | erase (T... args)
T | find (T... args)
T | find_first_not_of (T... args)
T | find_first_of (T... args)
T | find_last_not_of (T... args)
T | find_last_of (T... args)
T | front (T... args)
T | get_allocator (T... args)
T | insert (T... args)
T | length (T... args)
T | max_size (T... args)
T | operator= (T... args)
T | operator[] (T... args)
T | pop_back (T... args)
T | push_back (T... args)
T | rbegin (T... args)
T | rend (T... args)
T | replace (T... args)
T | reserve (T... args)
T | resize (T... args)
T | rfind (T... args)
T | shrink_to_fit (T... args)
T | size (T... args)
T | substr (T... args)
T | 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 << "emptyString = " << emptyString << '\n';
  std::cout << "string1 = " << string1 << '\n';
  std::cout << "string2 = " << string2 << '\n';
  std::cout << "string3 = " << string3 << '\n';
  std::cout << "string4 = " << string4 << '\n';
  std::cout << "emptyString size = " << emptyString.getSize() << '\n';
  std::cout << "string1 size = " << string1.getSize() << '\n';
  std::cout << "string1 length = " << string1.getLength() << '\n';
  const char* c_string = string1.getString();
  std::cout << "c_string = " << c_string << '\n';
  std::cout << " 3rd letter of string1 =" << string1[ 2 ] << '\n';
  std::cout << " string1 + string2 = " << string1 + string2 << '\n';
  std::cout << " string1 + \" another string\" = " << string1 + " another string\n";
  string2 += "another string";
  std::cout << " string2 = " << string2;
  string2 = "string 2";
  if( string3 == string2 )
  std::cout << "string3 == string2\n";
  if( string1 != string2 )
  std::cout << "string1 != string2\n";
  if( ! emptyString )
  std::cout << "emptyString is empty\n";
  if( string1 )
  std::cout << "string1 is not empty\n";
  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 << '\n';
  }
  std::cout
  TNL::File
  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
  TNL::File::close
  void close()
  Closes the file.
  TNL::File::open
  void open(const std::string &fileName, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
  Open given file.
  TNL::String::getSize
  int getSize() const
  Returns the number of characters in given string.
  TNL::String::String
  String()=default
  Default constructor.
  TNL
  The main TNL namespace.
  Definition AtomicOperations.h:15
  TNL::convertToString
  String convertToString(const T &value)
  Converts value of type T to a String.
  Definition String.h:409

Output
  emptyString =
  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 : std::uint8_t
| 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
| nodiscard

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() << '\n';
  }

Output
  Allocated_size = 15

## ◆ getString()

const char * TNL::String::getString | ( |  | ) | const
| nodiscard

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

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

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
| nodiscard

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
| nodiscard

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
| nodiscard

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
| nodiscard

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 | ) |

This function overloads operator+=.

Operators for single characters.

Appends character str to this string.

## ◆ operator+=() [2/4]

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

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 | ) |

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 | ) |

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
| nodiscard

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
| nodiscard

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
| nodiscard

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
| nodiscard

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 | ) |
| nodiscard

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
| nodiscard

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
| nodiscard

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

  pattern | to be replaced.
  replaceWith | string the pattern will be replaced with.
  count | number 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 ) << '\n';
  std::cout << "phrase.replace( \"yes\", \"no\", 2 ) = " << phrase.replace( "yes", "no", 2 ) << '\n';
  std::cout << "phrase.replace( \"yes\", \"no\", 3 ) = " << phrase.replace( "yes", "no", 3 ) << '\n';
  }

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 | ) |

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

  size | Number 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() << '\n';
  std::cout << "Allocated size = " << string.getAllocatedSize() << '\n';
  }
  TNL::String::string
  T string(T... args)
  Inherited constructors.

Output
  String size = 1024
  Allocated size = 1024

## ◆ split()

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

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

  separator | is 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 ] << '\n';
  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 ] << '\n';
  std::vector< String > list5 = cars.split( ',' );
  std::cout << "split without String::SkipEmpty = " << list5[ 0 ] << ", " << list5[ 1 ] << ", " << list5[ 2 ] << ", "
  << list5[ 3 ] << '\n';
  }
  TNL::String::SplitSkip::SkipEmpty
  @ SkipEmpty
  Skip empty characters.
  Definition String.h:39
  std::vector

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
| nodiscard

Trims/strips this string.

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

Parameters

  strip | can 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() << '\n';
  std::cout << "names2 strip is: " << names.strip( '.' ) << '\n';
  }

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 file:

- src/TNL/String.h