KStringHandler

KStringHandler Namespace Reference

Functions

KCOREADDONS_EXPORT QString capwords (const QString &text)
 
KCOREADDONS_EXPORT QStringList capwords (const QStringList &list)
 
KCOREADDONS_EXPORT QString csqueeze (const QString &str, int maxlen=40)
 
KCOREADDONS_EXPORT int logicalLength (const QString &text)
 
KCOREADDONS_EXPORT QString lsqueeze (const QString &str, int maxlen=40)
 
KCOREADDONS_EXPORT QString obscure (const QString &str)
 
KCOREADDONS_EXPORT QStringList perlSplit (const QChar &sep, const QString &s, int max=0)
 
KCOREADDONS_EXPORT QStringList perlSplit (const QRegularExpression &sep, const QString &s, int max=0)
 
KCOREADDONS_EXPORT QStringList perlSplit (const QString &sep, const QString &s, int max=0)
 
KCOREADDONS_EXPORT QStringList perlSplit (const QStringView sep, const QStringView str, int max)
 
KCOREADDONS_EXPORT QString preProcessWrap (const QString &text)
 
KCOREADDONS_EXPORT QString rsqueeze (const QString &str, int maxlen=40)
 
KCOREADDONS_EXPORT QString tagUrls (const QString &text)
 

Detailed Description

This namespace contains utility functions for handling strings.

The functions here are intended to provide an easy way to cut/slice/splice words inside sentences in whatever order desired. While the main focus of KStringHandler is words (ie characters separated by spaces/tabs), the two core functions here (split() and join()) will allow you to use any character as a separator This will make it easy to redefine what a 'word' means in the future if needed.

The function names and calling styles are based on python and mIRC's scripting support.

The ranges are a fairly powerful way of getting/stripping words from a string. These ranges function, for the large part, as they would in python. See the word(const QString&, int) and remword(const QString&, int) functions for more detail.

The methods here are completely stateless. All strings are cut on the fly and returned as new qstrings/qstringlists.

Namespace for manipulating words and sentences in strings

Author
Ian Zepp icsze.nosp@m.pp@i.nosp@m.slc.n.nosp@m.et
See also
KShell

Function Documentation

◆ capwords() [1/2]

QString KStringHandler::capwords ( const QString & text)

Capitalizes each word in the string "hello there" becomes "Hello There" (string)

Parameters
textthe text to capitalize
Returns
the resulting string

Definition at line 21 of file kstringhandler.cpp.

◆ capwords() [2/2]

QStringList KStringHandler::capwords ( const QStringList & list)

Capitalizes each word in the list [hello, there] becomes [Hello, There] (list)

Parameters
listthe list to capitalize
Returns
the resulting list

Definition at line 36 of file kstringhandler.cpp.

◆ csqueeze()

QString KStringHandler::csqueeze ( const QString & str,
int maxlen = 40 )

Substitute characters at the middle of a string by "...".

Parameters
stris the string to modify
maxlenis the maximum length the modified string will have If the original string is shorter than "maxlen", it is returned verbatim
Returns
the modified string

Definition at line 55 of file kstringhandler.cpp.

◆ logicalLength()

int KStringHandler::logicalLength ( const QString & text)

Returns the length that reflects the density of information in the text.

In general the character from CJK languages are assigned with weight 2, while other Latin characters are assigned with 1.

Since
5.41

Definition at line 234 of file kstringhandler.cpp.

◆ lsqueeze()

QString KStringHandler::lsqueeze ( const QString & str,
int maxlen = 40 )

Substitute characters at the beginning of a string by "...".

Parameters
stris the string to modify
maxlenis the maximum length the modified string will have If the original string is shorter than "maxlen", it is returned verbatim
Returns
the modified string

Definition at line 45 of file kstringhandler.cpp.

◆ obscure()

QString KStringHandler::obscure ( const QString & str)

Obscure string by using a simple symmetric encryption.

Applying the function to a string obscured by this function will result in the original string.

The function can be used to obscure passwords stored to configuration files. Note that this won't give you any more security than preventing that the password is directly copied and pasted.

Parameters
strstring to be obscured
Returns
obscured string

Definition at line 164 of file kstringhandler.cpp.

◆ perlSplit() [1/4]

QStringList KStringHandler::perlSplit ( const QChar & sep,
const QString & s,
int max = 0 )

Split a QString into a QStringList in a similar fashion to the static QStringList function in Qt, except you can specify a maximum number of tokens.

If max is specified (!= 0) then only that number of tokens will be extracted. The final token will be the remainder of the string.

Example:

perlSplit(' ', "kparts reaches the parts other parts can't", 3)
QStringList contains: "kparts", "reaches", "the parts other parts can't"
KCOREADDONS_EXPORT QStringList perlSplit(const QStringView sep, const QStringView str, int max)
Split a string into a QStringList in a similar fashion to the static QStringList function in Qt,...
Parameters
sepis the character to use to delimit s.
sis the input string
maxis the maximum number of extractions to perform, or 0.
Returns
A QStringList containing tokens extracted from s.

Definition at line 109 of file kstringhandler.cpp.

◆ perlSplit() [2/4]

QStringList KStringHandler::perlSplit ( const QRegularExpression & sep,
const QString & s,
int max = 0 )

Split a QString into a QStringList in a similar fashion to the static QStringList function in Qt, except you can specify a maximum number of tokens.

If max is specified (!= 0) then only that number of tokens will be extracted. The final token will be the remainder of the string.

Example:

perlSplit(QRegularExpression("[! ]"), "Split me up ! I'm bored ! OK ?", 3)
QStringList contains: "Split", "me", "up ! I'm bored ! OK ?"
Parameters
sepis the regular expression to use to delimit s.
sis the input string
maxis the maximum number of extractions to perform, or 0.
Returns
A QStringList containing tokens extracted from s.
Since
5.67

Definition at line 114 of file kstringhandler.cpp.

◆ perlSplit() [3/4]

QStringList KStringHandler::perlSplit ( const QString & sep,
const QString & s,
int max = 0 )

Split a QString into a QStringList in a similar fashion to the static QStringList function in Qt, except you can specify a maximum number of tokens.

If max is specified (!= 0) then only that number of tokens will be extracted. The final token will be the remainder of the string.

Example:

perlSplit("__", "some__string__for__you__here", 4)
QStringList contains: "some", "string", "for", "you__here"
Parameters
sepis the string to use to delimit s.
sis the input string
maxis the maximum number of extractions to perform, or 0.
Returns
A QStringList containing tokens extracted from s.

Definition at line 104 of file kstringhandler.cpp.

◆ perlSplit() [4/4]

QStringList KStringHandler::perlSplit ( const QStringView sep,
const QStringView str,
int max )

Split a string into a QStringList in a similar fashion to the static QStringList function in Qt, except you can specify a maximum number of tokens.

If max is specified (!= 0) then only that number of tokens will be extracted. The final token will be the remainder of the string.

Example:

perlSplit("__", "some__string__for__you__here", 4)
QStringList contains: "some", "string", "for", "you__here"
Parameters
sepis the string to use to delimit str
strthe string to split
maxthe maximum number of extractions to perform, or 0
Returns
A QStringList containing tokens extracted from str
Since
5.87

Definition at line 76 of file kstringhandler.cpp.

◆ preProcessWrap()

QString KStringHandler::preProcessWrap ( const QString & text)

Preprocesses the given string in order to provide additional line breaking opportunities for QTextLayout.

This is done by inserting ZWSP (Zero-width space) characters in the string at points that wouldn't normally be considered word boundaries by QTextLayout, but where wrapping the text will produce good results.

Examples of such points includes after punctuation signs, underscores and dashes, that aren't followed by spaces.

Since
4.4

Definition at line 188 of file kstringhandler.cpp.

◆ rsqueeze()

QString KStringHandler::rsqueeze ( const QString & str,
int maxlen = 40 )

Substitute characters at the end of a string by "...".

Parameters
stris the string to modify
maxlenis the maximum length the modified string will have If the original string is shorter than "maxlen", it is returned verbatim
Returns
the modified string

Definition at line 66 of file kstringhandler.cpp.

◆ tagUrls()

QString KStringHandler::tagUrls ( const QString & text)

This method auto-detects URLs in strings, and adds HTML markup to them so that richtext or HTML-enabled widgets will display the URL correctly.

Parameters
textthe string which may contain URLs
Returns
the resulting text

Definition at line 153 of file kstringhandler.cpp.

This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:22 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.