Logo    
KDWinUtils
Helper library for MFC to Qt migration
Loading...
Searching...
No Matches
KString Class Reference

The KString class provides a Unicode character string. More...

#include <kstring.h>

Inheritance diagram for KString:
[legend]
Collaboration diagram for KString:
[legend]

Public Member Functions

int Collate (const KString &other) const
 Compares two strings according to a localized collation algorithm.
 
int CollateNoCase (const KString &other) const
 Compares two strings according to a localized collation algorithm.
 
int Compare (const KString &other) const
 Compares two strings (case-sensitive).
 
int CompareNoCase (const KString &other) const
 Compares two strings (case-insensitive).
 
int Delete (int iIndex, int nCount=1)
 Deletes a character or characters from a string starting with the character at the given index.
 
void Empty ()
 Forces a string to have 0 length.
 
int Find (const KString &other, int iStart=0) const
 Finds a character or substring inside a larger string.
 
int FindOneOf (const KString &charSet) const
 Searches this string for the first character that matches any character contained in charSet.
 
template<typename... Ts>
KStringFormat (const wchar_t *format, Ts &&...args)
 Formats the string as sprintf does.
 
bool GetEnvironmentVariable (const KString &envVar)
 Sets the string to the value of the specified environment variable.
 
int GetLength () const
 Returns the number of characters in a KString object.
 
int Insert (int iIndex, const KString &string)
 Inserts a substring at the given index within the string.
 
int Insert (int iIndex, QChar character)
 Inserts a single character at the given index within the string.
 
bool IsEmpty () const
 Tests whether a CString object contains no characters.
 
 KString ()
 
 KString (const CString &str)
 
 KString (const QString &str)
 
 KString (const std::wstring_view str)
 
 KString (const wchar_t *text)
 
 KString (QString &&str) noexcept
 
KString Left (int nCount) const
 Extracts the leftmost nCount characters from this string object.
 
KStringMakeLower ()
 Converts all the characters in this string to lowercase characters.
 
KStringMakeReverse ()
 Reverses the order of the characters in the KString object.
 
KStringMakeUpper ()
 Converts all the characters in this string to uppercase characters.
 
KString Mid (int iFirst, int nCount=-1) const
 Extracts a substring of length nCount characters from this KString object, starting at position iFirst (zero-based).
 
 operator const char * () const
 Converts to a const char * implicitly.
 
 operator CString () const
 
KStringoperator+= (const char *str)
 
KStringoperator+= (const CString &str)
 
KStringoperator+= (const QString &str)
 
KStringoperator+= (const wchar_t *str)
 
KStringoperator+= (QStringView str)
 
KStringoperator= (const char *str)
 
KStringoperator= (const CString &str)
 
KStringoperator= (const QString &str)
 
KStringoperator= (const std::wstring_view str)
 
KStringoperator= (const wchar_t *str)
 
KStringoperator= (QString &&str) noexcept
 
int Remove (QChar chRemove)
 Removes all instances of the specified character from the string.
 
int Replace (const KString &before, const KString &after)
 Replaces a substring by another substring. Both substrings are null-terminated.
 
int Replace (QChar before, QChar after)
 Replaces a character by another character.
 
int ReverseFind (QChar ch)
 Searches this KString object for the last match of a character.
 
KString Right (int nCount) const
 Extracts the rightmost nCount characters from this string object.
 
KString SpanExcluding (const KString &excludeChars) const
 Extracts characters from the string, starting with the first character, that aren't in the set of characters identified by excludeChars.
 
KString SpanIncluding (const KString &includeChars) const
 Extracts characters from the string, starting with the first character, that are in the set of characters identified by includeChars.
 
CString toCString () const
 
KString Tokenize (const KString &tokens, int &iStart) const
 Finds the next token in a target string.
 
KStringTrim ()
 Trims leading and trailing whitespace from the string.
 
KStringTrim (const KString &charsTarget)
 Trims leading and trailing characters from the string.
 
KStringTrim (QChar chTarget)
 Trims leading and trailing character from the string.
 
KStringTrimLeft ()
 Trims leading whitespace from the string.
 
KStringTrimLeft (const KString &charsTarget)
 Trims leading characters from the string.
 
KStringTrimLeft (QChar charTarget)
 Trims leading character from the string.
 
KStringTrimRight ()
 Trims trailing whitespace from the string.
 
KStringTrimRight (const KString &charsTarget)
 Trims trailing characters from the string.
 
KStringTrimRight (QChar charTarget)
 Trims trailing character from the string.
 
 ~KString ()
 

Static Public Member Functions

template<typename T , typename... Ts>
static QString FormatString (T &&format, Ts... args)
 Creates a QString by passing a format string and arguments, like CString::Format.
 

Related Symbols

(Note that these are not member symbols.)

KString operator+ (const KString &s1, const KString &s2)
 Concatenate two strings.
 

Detailed Description

The KString class provides a Unicode character string.

This class can replace CString (unicode or non-unicode) with the same API, and is compatible with both CString and QString.

Warning
Do not store a KString in a QString*, as the KString destructor won't be called.
See also
https://learn.microsoft.com/en-us/cpp/atl-mfc-shared/reference/cstringt-class

Constructor & Destructor Documentation

◆ KString() [1/6]

KString::KString ( )

◆ ~KString()

KString::~KString ( )

◆ KString() [2/6]

KString::KString ( const wchar_t * text)

◆ KString() [3/6]

KString::KString ( const QString & str)

◆ KString() [4/6]

KString::KString ( QString && str)
noexcept

◆ KString() [5/6]

KString::KString ( const std::wstring_view str)

◆ KString() [6/6]

KString::KString ( const CString & str)
explicit

Member Function Documentation

◆ Collate()

int KString::Collate ( const KString & other) const

Compares two strings according to a localized collation algorithm.

Does a case-sensitive comparison of the strings.

◆ CollateNoCase()

int KString::CollateNoCase ( const KString & other) const

Compares two strings according to a localized collation algorithm.

Does a case-insensitive comparison of the strings.

◆ Compare()

int KString::Compare ( const KString & other) const

Compares two strings (case-sensitive).

◆ CompareNoCase()

int KString::CompareNoCase ( const KString & other) const

Compares two strings (case-insensitive).

◆ Delete()

int KString::Delete ( int iIndex,
int nCount = 1 )

Deletes a character or characters from a string starting with the character at the given index.

Parameters
iIndex.The zero-based index of the first character in the KString object to delete.
nCount.The number of characters to be removed.
Returns
The length of the changed string.

◆ Empty()

void KString::Empty ( )

Forces a string to have 0 length.

◆ Find()

int KString::Find ( const KString & other,
int iStart = 0 ) const

Finds a character or substring inside a larger string.

◆ FindOneOf()

int KString::FindOneOf ( const KString & charSet) const

Searches this string for the first character that matches any character contained in charSet.

Parameters
charSet.String containing characters for matching.
Returns
The zero-based index of the first character in this string that is also in charSet; -1 if there is no match.

◆ Format()

template<typename... Ts>
KString & KString::Format ( const wchar_t * format,
Ts &&... args )
inline

Formats the string as sprintf does.

◆ FormatString()

template<typename T , typename... Ts>
QString KString::FormatString ( T && format,
Ts... args )
static

Creates a QString by passing a format string and arguments, like CString::Format.

◆ GetEnvironmentVariable()

bool KString::GetEnvironmentVariable ( const KString & envVar)

Sets the string to the value of the specified environment variable.

Parameters
envVar.Pointer to a null-terminated string that specifies the environment variable.
Returns
Nonzero if successful; otherwise 0.

Retrieves the value of the specified variable from the environment block of the calling process. The value is in the form of a null-terminated string of characters.

◆ GetLength()

int KString::GetLength ( ) const

Returns the number of characters in a KString object.

For multibyte characters, counts each 16-bit character; that is, a lead and trail byte in one multibyte character are counted as two characters. This also known as a "surrogate pair".

◆ Insert() [1/2]

int KString::Insert ( int iIndex,
const KString & string )

Inserts a substring at the given index within the string.

Parameters
iIndex.The index of the character before which the insertion will take place.
string.The substring to be inserted.
Returns
The length of the changed string.

◆ Insert() [2/2]

int KString::Insert ( int iIndex,
QChar character )

Inserts a single character at the given index within the string.

Parameters
iIndex.The index of the character before which the insertion will take place.
character.The character to be inserted.
Returns
The length of the changed string.

◆ IsEmpty()

bool KString::IsEmpty ( ) const

Tests whether a CString object contains no characters.

◆ Left()

KString KString::Left ( int nCount) const

Extracts the leftmost nCount characters from this string object.

Returns
A copy of the extracted substring.

◆ MakeLower()

KString & KString::MakeLower ( )

Converts all the characters in this string to lowercase characters.

◆ MakeReverse()

KString & KString::MakeReverse ( )

Reverses the order of the characters in the KString object.

Returns
The resulting reversed string.

◆ MakeUpper()

KString & KString::MakeUpper ( )

Converts all the characters in this string to uppercase characters.

◆ Mid()

KString KString::Mid ( int iFirst,
int nCount = -1 ) const

Extracts a substring of length nCount characters from this KString object, starting at position iFirst (zero-based).

If nCount isn't supplied, then the rest of the string is extracted.

◆ operator const char *()

KString::operator const char * ( ) const
explicit

Converts to a const char * implicitly.

This method is used mainly for compatibility for old API or code. This method requires a QByteArray internally, to be avoided if possible.

Deprecated
Use explicit conversion methods

◆ operator CString()

KString::operator CString ( ) const

◆ operator+=() [1/5]

KString & KString::operator+= ( const char * str)

◆ operator+=() [2/5]

KString & KString::operator+= ( const CString & str)

◆ operator+=() [3/5]

KString & KString::operator+= ( const QString & str)

◆ operator+=() [4/5]

KString & KString::operator+= ( const wchar_t * str)

◆ operator+=() [5/5]

KString & KString::operator+= ( QStringView str)

◆ operator=() [1/6]

KString & KString::operator= ( const char * str)

◆ operator=() [2/6]

KString & KString::operator= ( const CString & str)

◆ operator=() [3/6]

KString & KString::operator= ( const QString & str)

◆ operator=() [4/6]

KString & KString::operator= ( const std::wstring_view str)

◆ operator=() [5/6]

KString & KString::operator= ( const wchar_t * str)

◆ operator=() [6/6]

KString & KString::operator= ( QString && str)
noexcept

◆ Remove()

int KString::Remove ( QChar chRemove)

Removes all instances of the specified character from the string.

Returns
The count of characters removed from the string. Zero if the string isn't changed.

Comparisons for the character are case-sensitive.

◆ Replace() [1/2]

int KString::Replace ( const KString & before,
const KString & after )

Replaces a substring by another substring. Both substrings are null-terminated.

Returns
The number of replaced instances of the substring, or zero if the string isn't changed.
Remarks
The function does a case-sensitive match.

◆ Replace() [2/2]

int KString::Replace ( QChar before,
QChar after )

Replaces a character by another character.

Returns
The number of replaced instances of the character, or zero if the string isn't changed.
Remarks
The function does a case-sensitive match.

◆ ReverseFind()

int KString::ReverseFind ( QChar ch)

Searches this KString object for the last match of a character.

Returns
The zero-based index of the last character in this kString object that matches the requested character, or -1 if the character isn't found.

◆ Right()

KString KString::Right ( int nCount) const

Extracts the rightmost nCount characters from this string object.

Returns
A copy of the extracted substring.

◆ SpanExcluding()

KString KString::SpanExcluding ( const KString & excludeChars) const

Extracts characters from the string, starting with the first character, that aren't in the set of characters identified by excludeChars.

Returns
all characters preceding the first occurrence of a character from excludeChars.

If no character from excludeChars is found in the string, then returns the entire string.

◆ SpanIncluding()

KString KString::SpanIncluding ( const KString & includeChars) const

Extracts characters from the string, starting with the first character, that are in the set of characters identified by includeChars.

Returns
A substring that contains characters in the string that are in includeChars, beginning with the first character in the string and ending when a character is found in the string that isn't in includeChars.

If the first character of the string isn't in the character set, then SpanIncluding returns an empty string. Otherwise, it returns a sequence of consecutive characters that are in the set.

◆ toCString()

CString KString::toCString ( ) const

◆ Tokenize()

KString KString::Tokenize ( const KString & tokens,
int & iStart ) const

Finds the next token in a target string.

Parameters
tokens.A string containing token delimiters. The order of these delimiters isn't important.
iStart.The zero-based index to begin the search.
Returns
A KString object containing the current token value.
Remarks
The value of iStart is updated to be the position following the ending delimiter character, or -1 if the end of the string was reached.

◆ Trim() [1/3]

KString & KString::Trim ( )

Trims leading and trailing whitespace from the string.

◆ Trim() [2/3]

KString & KString::Trim ( const KString & charsTarget)

Trims leading and trailing characters from the string.

◆ Trim() [3/3]

KString & KString::Trim ( QChar chTarget)

Trims leading and trailing character from the string.

◆ TrimLeft() [1/3]

KString & KString::TrimLeft ( )

Trims leading whitespace from the string.

◆ TrimLeft() [2/3]

KString & KString::TrimLeft ( const KString & charsTarget)

Trims leading characters from the string.

◆ TrimLeft() [3/3]

KString & KString::TrimLeft ( QChar charTarget)

Trims leading character from the string.

◆ TrimRight() [1/3]

KString & KString::TrimRight ( )

Trims trailing whitespace from the string.

◆ TrimRight() [2/3]

KString & KString::TrimRight ( const KString & charsTarget)

Trims trailing characters from the string.

◆ TrimRight() [3/3]

KString & KString::TrimRight ( QChar charTarget)

Trims trailing character from the string.

Friends And Related Symbol Documentation

◆ operator+()

KString operator+ ( const KString & s1,
const KString & s2 )
related

Concatenate two strings.


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