Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
UppHub
Status & Roadmap
FAQ
Authors & License
Forums
Funding U++
Search on this site











SourceForge.net Logo

SourceForge.net Logo

GitHub Logo

Discord Logo

String

 

class String : public Moveable<String, AString<String0> > 

String is a value class that contains an array of characters (or bytes). It is designed to have fast copy operations. The array of characters is zero terminated. String provides non-mutable access to the array of characters. String can store 8 bit encoded string or an UTF-8 encoded string. For UTF-8 strings, however, String works with raw 8 bit values - there are not automatic conversions performed e.g. in operator[]. If you need to access individual UTF-8 characters, the best practice in most cases is to convert it to WString (and eventually back after processing).

String is also often used to store raw binary data.

WString is similar to String, but it uses 32 bit encoding. String and WString share a number of common functions through the AString interface.

 

 

Constructor Detail

 

String()

Default constructor. Constructs empty String.

 


 

String(const String& s)

Default copy constructor.

 


 

String(String&& s)

Pick constructor.

 


 

String(const char *s)

Creates a copy of zero terminated string s.

 


 

String(const char *s, int n)

Constructs a string equal to first n characters of string s. Zero characters are included.

 


 

String(const String& s, int n)

Constructs a string equal to first n characters of String s.  Zero characters are included.

 


 

String(const byte *s, int n)

Constructs a string equal to first n bytes of s. Zero characters are included.

 


 

String(const char *s, const char *lim)

Constructs a string equal to first lim - s characters of s. Zero characters are included.

 


 

String(int chr, int count)

Constructs a string consisting of count characters equal to chr. chr can be zero.

 


 

String(StringBuffer& b)

Constructs a string from b. b is emptied by this operation.

 


 

String(const Nuller&)

Constructs empty String. This variant is important to allow assigning Null to String (which is same as assigning the empty String).

 


 

String(const std::string& s)

Constructor to achieve minimal STL compatibility.

 

 

Public Member List

 

void Shrink()

Reduces memory used by string to minimum (size optimization).

 


 

int GetCharCount() const

Returns a number of characters contained in String. This is equal GetCount() if default charset is not UTF-8, but different for UTF-8 where it returns a number of unicode codepoints. It is faster equivalent of ToWString().GetCount().

 


 

WString ToWString() const

Convert String to WString using current default character set.

 


 

const String& ToString() const

Returns *this. Useful because ToString is standard method to convert concrete type to text, used by AsString template (and therefore by operator << as well).

 


 

static String GetVoid()

Returns special Void value. This value is same as empty string, but IsVoid method returns true for it. It can be used in special scenarios where client code needs to distinguish between two empty values (let us say Void is "more empty"...). For example, LoadFile returns Void string if file failed to load, but normal empty string if it is empty.

 


 

bool IsVoid() const

Returns true if String is special value returned by GetVoid.

 


 

bool IsEqual(const String& sconst

Return true if string is equal to s.

 


 

int Compare(const String0& sconst

Lexicographic comparison, return -1 if this string is lower, 0 for equality, 1 if this string is greater. Individual characters are compared as unsigned integer values.

 


 

unsigned GetHashValue() const

Returns the hash value of the string.

 


 

void Cat(int c)

const String& operator+=(char c)

Appends single character c. This operations has constant amortized time (in other words, internal space used by implementation grows exponentially, like with Vector or std::vector). c can be zero.

 


 

void Cat(const char *s, int len)

Appends len characters from string s (s can contain zero characters).

 


 

void Set(int i, int chr)

Replace character at position i with chr.

 


 

void Trim(int pos)

Sets the number of characters (length) to pos.

 


 

const char *Begin() const

Returns a pointer to the first character.

 


 

const char *End() const

Returns a pointer to ending zero.

 


 

void Remove(int pos, int count = 1)

Removes count element starting at position pos.

 


 

void Clear()

Empties the string.

 


 

int GetCount() const

int GetLength() const

Returns the number of characters.

 


 

int GetAlloc() const

Returns the number of allocated characters (maximum string length before it has to grow)

 


 

void Reserve(int r)

Reserves r characters of internal storage for future concatenation operations.

 


 

const String& operator+=(const char *s)

const String& operator+=(const String& s)

Appends a zero terminated string s.

 


 

String& operator=(const String& s)

Assigns another string s. This operations is relatively fast and does not depend on the length of string.

 


 

String& operator=(String&& s)

Pick assignment.

 


 

String& operator=(const char *s)

Assign a zero terminated string s.

 


 

String& operator=(StringBuffer& b)

Assigns the content of StringBuffer. After the operation,b is emptied.

 


 

String& operator<<=(const String& s)

"Deep" assignment. It is equivalent of standard assignment followed by Shrink operation (in other words, internal buffer gets reallocated to the exact size of source).

 


 

std::string ToStd() const

Converts String to std::string.

 


 

operator const char *() const

const char *operator~() const

Returns Begin().

 


 

int operator[](int iconst

Returns the character at position i.

 


 

template <class Makerstatic String Make(int alloc, Maker m)

Optimized static method for creating Strings. This method creates internal buffer of at least alloc and then invokes lambda m passing the char * pointer to the internal buffer as lambda parameter. Lambda is then supposed to fill the characters to this buffer and return the length of string (which must be <= alloc). For the best performance, alloc should be constant.

 

 

WString

 

class WString : public Moveable<WString, AString<WString0> > 

WString is similar to String, but it uses 16 bit Ucs2 encoding. String and WString share a number of common functions through the AString interface.

 

 

Constructor Detail

 

WString()

Default constructor. Constructs empty WString.

 


 

WString(const WString& s)

Default copy constructor.

 


 

WString(const wchar *s)

Creates a copy of zero terminated string s.

 


 

WString(const char *s)

Creates a copy of zero terminated string s.

 


 

WString(const WString& s, int n)

Constructs a string equal to first n characters of WString s (s can contain zero characters).

 


 

WString(const wchar *s, int n)

Constructs a string equal to first n characters of s (s can contain zero characters).

 


 

WString(const wchar *s, const wchar *lim)

Constructs a string equal to first lim - s characters of s (s can contain zero characters).

 


 

WString(const char *s, int n)

Constructs a string equal to first n characters of zero terminated string s (s can contain zero characters).

 


 

WString(const char *s, const char *lim)

Constructs a string equal to first n characters of zero terminated string s (s can contain zero characters).

 


 

WString(int chr, int count)

Constructs a string consisting of count characters equal to chr. chr can be zero.

 


 

WString(WStringBuffer& b)

Constructs a string from b. b is emptied by this operation.

 


 

WString(const Nuller&)

Constructs empty WString. This variant is important to allow assigning Null to WString (which is same as assigning the empty WString).

 


 

WString(const std::wstring& s)

Constructor to achieve minimal STL compatibility.

 

 

Public Member List

 

void Shrink()

Reduces memory used by string to minimum (size optimization).

 


 

String ToString() const

Converts WString to String using current default character set. Also serves as standard text conversion (for AsString and operator<< templates).

 


 

static WString GetVoid()

Returns special Void value. This value is same as empty string, but IsVoid method returns true for it. It can be used in special scenarios where client code needs to distinguish between two empty values (let us say Void is "more empty"...). For example, LoadFile returns Void string if file failed to load, but normal empty string if it is empty.

 


 

bool IsVoid() const

Returns true if String is special value returned by GetVoid.

 


 

const wchar *Begin() const

Returns a pointer to the first character.

 


 

const wchar *End() const

Returns a pointer to ending zero.

 


 

int GetCount() const

int GetLength() const

Returns the number of characters.

 


 

int GetAlloc() const

Returns the number of allocated characters (maximum string length before it has to grow)

 


 

void Cat(int c)

Appends single character c. This operations has constant amortized time (in other words, internal space used by implementation grows exponentially, like with Vector or std::vector).

 


 

void Cat(const wchar *s, int length)

Appends len characters from string s (s can contain zero characters).

 


 

hash_t GetHashValue() const

Returns the hash value of the string.

 


 

bool IsEqual(const WString& sconst

Return true if string is equal to s.

 


 

int Compare(const WString& sconst

Lexicographic comparison, return -1 if this string is lower, 0 for equality, 1 if this string is greater. Individual characters are compared as unsigned integer values.

 


 

void Remove(int pos, int count = 1)

Removes count element starting at position pos.

 


 

void Insert(int pos, const wchar *s, int count)

Inserts first count characters of s at position pos.

 


 

void Clear()

Empties the string.

 


 

void Set(int pos, int ch)

Replace character at position pos  with ch.

 


 

void Trim(int pos)

Sets the number of characters (length) to pos.

 


 

std::wstring ToStd() const

Converts WString to std::string.

 

 

AString

 

template <class B>

class AString : public B

A class that implements behavior common to both String and WString.

 

 

Public Member List

 

void Clear()

Clears the content of the string.

 


 

int GetLength() const

Returns the length of the string.

 


 

bool IsEmpty() const

Return true if the length of the string is zero.

 


 

const tchar *End() const

Returns a pointer to the end of the string.

 


 

const tchar *Last() const

Returns a pointer to the last character in the string. If string is empty, returns a pointer to the ending zero.

 


 

const tchar *GetIter(int iconst

Returns a pointer to character index i of the string.

 


 

int operator[](int iconst

Returns the character with index i.

 


 

operator const tchar *() const

const tchar *operator~() const

operator const void *() const

operator const bchar *() const

Same as Begin().

 


 

void Insert(int pos, int c)

void Insert(int pos, const tchar *s, int count)

void Insert(int pos, const String& s)

void Insert(int pos, const char *s)

Inserts item c/s at position pos (count times).

 


 

void TrimLast(int count = 1)

Removes count characters from the end of String. Same as Remove(GetCount() - count).

 


 

String Mid(int pos, int lengthconst

Returns a substring that begins from pos and with length chars.

 


 

String Mid(int posconst

Returns a substring that begins from pos.

 


 

String Right(int countconst

Returns a substring with count chars beginning from the ens of the string.

 


 

String Left(int countconst

Returns a substring with count chars beginning from the begin of the string.

 


 

int Find(int len, const tchar *s, int fromconst

int Find(const tchar *s, int from = 0const

int Find(const String& s, int from = 0const

Returns first position of substring s greater than or equal to from, or -1 if s is not found.

 


 

int FindAfter(const tchar *s, int from = 0const

int FindAfter(const String& s, int from = 0const

Similiar to Find, but if found, returns position after the substring found. In other words, if not found returns -1, if found, returns Find(s, from) + length of s.

 


 

int ReverseFind(int len, const tchar *s, int fromconst

int ReverseFind(const tchar *s, int fromconst

int ReverseFind(const String& s, int fromconst

int ReverseFind(const tchar *sconst

int ReverseFind(const String& sconst

Finds the last position of s less than from, or -1 if not found.

 


 

int ReverseFindAfter(int len, const tchar *s, int fromconst

int ReverseFindAfter(const tchar *s, int fromconst

int ReverseFindAfter(const String& s, int fromconst

int ReverseFindAfter(const tchar *sconst

int ReverseFindAfter(const String& sconst

Similar to ReverseFind, but returns position after the substring found. In other words, if not found returns -1, if found, returns Find(s, from) + length of s.

 


 

void Replace(const tchar *find, int findlen, const tchar *replace, int replacelen)

void Replace(const String& find, const String& replace)

void Replace(const tchar *find, const tchar *replace)

void Replace(const String& find, const tchar *replace)

void Replace(const tchar *find, const String& replace)

Replaces substring find with replace string for all times find string appears.

 


 

bool StartsWith(const tchar *s, int lenconst

bool StartsWith(const tchar *sconst

bool StartsWith(const String& sconst

Returns true if string starts with s.

 


 

bool TrimStart(const tchar *s, int len)

bool TrimStart(const tchar *s)

bool TrimStart(const String& s)

If string starts with s, trims the start to remove it and returns true.

 


 

bool EndsWith(const tchar *s, int lenconst

bool EndsWith(const tchar *sconst

bool EndsWith(const String& sconst

Returns true if string ends with s.

 


 

bool TrimEnd(const tchar *s, int len)

bool TrimEnd(const tchar *s)

bool TrimEnd(const String& s)

If string ends with s, trims the end to remove it and returns true.

 


 

int FindFirstOf(int len, const tchar *set, int fromconst

Returns the first position of any character from set starting at from. The number of characters in set is len. Returns -1 if not found.

 


 

int FindFirstOf(const tchar *set, int from = 0const

Returns the first position of any character from zero terminated set starting at from. Returns -1 if not found.

 


 

int FindFirstOf(const String& set, int from = 0const

Returns the first position of any character from set starting at from. Returns -1 if not found.

 

 

 

Do you want to contribute?