Class: QgsStringUtils

class qgis.core.QgsStringUtils

Bases: sip.wrapper

Utility functions for working with strings.

New in version 2.11:

Methods

ampersandEncode

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

capitalize

Converts a string by applying capitalization rules to the string.

fuzzyScore

Tests a candidate string to see how likely it is a match for a specified search string.

hammingDistance

Returns the Hamming distance between two strings.

htmlToMarkdown

Convert simple HTML to markdown.

insertLinks

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

isUrl

Returns whether the string is a URL (http,https,ftp,file)

levenshteinDistance

Returns the Levenshtein edit distance between two strings.

longestCommonSubstring

Returns the longest common substring between two strings.

qRegExpEscape

Returns an escaped string matching the behavior of QRegExp.escape.

soundex

Returns the Soundex representation of a string.

substituteVerticalCharacters

Returns a string with characters having vertical representation form substituted.

truncateMiddleOfString

Truncates a string to the specified maximum character length.

wordWrap

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

Attributes

AllLowercase

AllUppercase

ForceFirstLetterToCapital

MixedCase

TitleCase

UpperCamelCase

AllLowercase = 2
AllUppercase = 1
class Capitalization

Bases: int

ForceFirstLetterToCapital = 4
MixedCase = 0
TitleCase = 1004
UpperCamelCase = 1005
ampersandEncode(string: str) str

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

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

New in version 3.2.

Parameters:

string (str) –

Return type:

str

capitalize(string: str, capitalization: QgsStringUtils.Capitalization) str

Converts a string by applying capitalization rules to the string.

Parameters:
Return type:

str

Returns:

capitalized string

New in version 3.0.

fuzzyScore(candidate: str, search: str) float

Tests a candidate string to see how likely it is a match for a specified search string. Values are normalized between 0 and 1.

Parameters:
  • candidate (str) – candidate string

  • search (str) – search term string

Return type:

float

Returns:

Normalized value of how likely is the search to be in the candidate

Note

Use this function only to calculate the fuzzy score between two strings and later compare these values, but do not depend on the actual numbers. They are implementation detail that may change in a future release.

New in version 3.14.

hammingDistance(string1: str, string2: str, caseSensitive: bool = False) int

Returns the Hamming distance between two strings. 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.

Parameters:
  • string1 (str) – first string

  • string2 (str) – second string

  • caseSensitive (bool = False) – set to True for case sensitive comparison

Return type:

int

Returns:

Hamming distance between strings, or -1 if strings are different lengths.

htmlToMarkdown(html: str) str

Convert simple HTML to markdown. Only br, b and link are supported.

Parameters:

html (str) – HTML to convert to markdown

Return type:

str

Returns:

String formatted as markdown

New in version 3.10.

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

Parameters:
  • string (str) – string to insert links into

  • foundLinks – if specified, will be set to True if any links were inserted into the string

Return type:

Tuple[str, bool]

Returns:

string with inserted links

New in version 3.0.

isUrl(string: str) bool

Returns whether the string is a URL (http,https,ftp,file)

Parameters:

string (str) – the string to check

Return type:

bool

Returns:

whether the string is an URL

New in version 3.22.

levenshteinDistance(string1: str, string2: str, caseSensitive: bool = False) int

Returns the Levenshtein edit distance between two strings. This equates to the minimum number of character edits (insertions, deletions or substitutions) required to change one string to another.

Parameters:
  • string1 (str) – first string

  • string2 (str) – second string

  • caseSensitive (bool = False) – set to True for case sensitive comparison

Return type:

int

Returns:

edit distance. Lower distances indicate more similar strings.

longestCommonSubstring(string1: str, string2: str, caseSensitive: bool = False) str

Returns the longest common substring between two strings. 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”.

Parameters:
  • string1 (str) – first string

  • string2 (str) – second string

  • caseSensitive (bool = False) – set to True for case sensitive comparison

Return type:

str

Returns:

longest common substring

qRegExpEscape(string: str) str

Returns an escaped string matching the behavior of QRegExp.escape.

Parameters:

string (str) – String to escape

Return type:

str

Returns:

Escaped string

New in version 3.22.

soundex(string: str) str

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

Parameters:

string (str) – input string

Return type:

str

Returns:

4 letter Soundex code

substituteVerticalCharacters(string: str) str

Returns a string with characters having vertical representation form substituted.

Parameters:

string (str) – input string

Return type:

str

Returns:

string with substitution applied

New in version 3.10.

truncateMiddleOfString(string: str, maxLength: int) str

Truncates a string to the specified maximum character length.

If the string exceeds the maximum character length, then the string will be truncated by removing characters from the middle of the string and replacing them with a horizontal ellipsis character.

New in version 3.22.

Parameters:
  • string (str) –

  • maxLength (int) –

Return type:

str

wordWrap(string: str, length: int, useMaxLineLength: bool = True, customDelimiter: str = '') str

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.

New in version 3.4.

Parameters:
  • string (str) –

  • length (int) –

  • useMaxLineLength (bool = True) –

  • customDelimiter (str = '') –

Return type:

str