QgsStringUtils class

Utility functions for working with strings.

Public types

enum Capitalization { MixedCase = QFont::MixedCase, AllUppercase = QFont::AllUppercase, AllLowercase = QFont::AllLowercase, ForceFirstLetterToCapital = QFont::Capitalize, TitleCase = QFont::Capitalize + 1000 }
Capitalization options.

Public static functions

static auto ampersandEncode(const QString& string) -> QString
Makes a raw string safe for inclusion as a HTML/XML string literal.
static auto capitalize(const QString& string, Capitalization capitalization) -> QString
Converts a string by applying capitalization rules to the string.
static auto hammingDistance(const QString& string1, const QString& string2, bool caseSensitive = false) -> int
Returns the Hamming distance between two strings.
static auto insertLinks(const QString& string, bool* foundLinks = nullptr) -> QString
Returns a string with any URL (e.g., http(s)/ftp) and mailto: text converted to valid HTML <a ...> links.
static auto levenshteinDistance(const QString& string1, const QString& string2, bool caseSensitive = false) -> int
Returns the Levenshtein edit distance between two strings.
static auto longestCommonSubstring(const QString& string1, const QString& string2, bool caseSensitive = false) -> QString
Returns the longest common substring between two strings.
static auto soundex(const QString& string) -> QString
Returns the Soundex representation of a string.
static auto wordWrap(const QString& string, int length, bool useMaxLineLength = true, const QString& customDelimiter = QString()) -> QString
Automatically wraps a string by inserting new line characters at appropriate locations in the string.

Enum documentation

enum QgsStringUtils::Capitalization

Capitalization options.

Enumerators
MixedCase

Mixed case, ie no change.

AllUppercase

Convert all characters to uppercase.

AllLowercase

Convert all characters to lowercase.

ForceFirstLetterToCapital

Convert just the first letter of each word to uppercase, leave the rest untouched.

TitleCase

Simple title case conversion - does not fully grammatically parse the text and uses simple rules only. Note that this method does not convert any characters to lowercase, it only uppercases required letters. Callers must ensure that input strings are already lowercased.

Function documentation

static QString QgsStringUtils::ampersandEncode(const QString& string)

Makes a raw string safe for inclusion as a HTML/XML string literal.

This includes replacing '<' with '<', '>' with '>', '&' with '&amp', and any extended unicode characters with the XML style &#233; encoded versions of these characters.

static QString QgsStringUtils::capitalize(const QString& string, Capitalization capitalization)

Converts a string by applying capitalization rules to the string.

Parameters
string input string
capitalization capitalization type to apply
Returns capitalized string

static int QgsStringUtils::hammingDistance(const QString& string1, const QString& string2, bool caseSensitive = false)

Returns the Hamming distance between two strings.

Parameters
string1 first string
string2 second string
caseSensitive set to true for case sensitive comparison
Returns Hamming distance between strings, or -1 if strings are different lengths.

This equates to the number of characters at corresponding positions within the input strings where the characters are different. The input strings must be the same length.

static QString QgsStringUtils::insertLinks(const QString& string, bool* foundLinks = nullptr)

Returns a string with any URL (e.g., http(s)/ftp) and mailto: text converted to valid HTML <a ...> links.

Parameters
string string to insert links into
foundLinks if specified, will be set to true if any links were inserted into the string
Returns string with inserted links

static int QgsStringUtils::levenshteinDistance(const QString& string1, const QString& string2, bool caseSensitive = false)

Returns the Levenshtein edit distance between two strings.

Parameters
string1 first string
string2 second string
caseSensitive set to true for case sensitive comparison
Returns edit distance. Lower distances indicate more similar strings.

This equates to the minimum number of character edits (insertions, deletions or substitutions) required to change one string to another.

static QString QgsStringUtils::longestCommonSubstring(const QString& string1, const QString& string2, bool caseSensitive = false)

Returns the longest common substring between two strings.

Parameters
string1 first string
string2 second string
caseSensitive set to true for case sensitive comparison
Returns longest common substring

This substring is the longest string that is a substring of the two input strings. For example, the longest common substring of "ABABC" and "BABCA" is "ABC".

static QString QgsStringUtils::soundex(const QString& string)

Returns the Soundex representation of a string.

Parameters
string input string
Returns 4 letter Soundex code

Soundex is a phonetic matching algorithm, so strings with similar sounds should be represented by the same Soundex code.

static QString QgsStringUtils::wordWrap(const QString& string, int length, bool useMaxLineLength = true, const QString& customDelimiter = QString())

Automatically wraps a string by inserting new line characters at appropriate locations in the string.

The length argument specifies either the minimum or maximum length of lines desired, depending on whether useMaxLineLength is true. If useMaxLineLength is true, then the string will be wrapped so that each line ideally will not exceed length of characters. If useMaxLineLength is false, then the string will be wrapped so that each line will ideally exceed length of characters.

A custom delimiter can be specified to use instead of space characters.