• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

Static Public Member Functions | List of all members
KStringHandler Class Reference

#include <kstringhandler.h>

Static Public Member Functions

static TQString word (const TQString &text, uint pos) TDE_DEPRECATED
 
static TQString word (const TQString &text, const char *range)
 
static TQString insword (const TQString &text, const TQString &word, uint pos)
 
static TQString setword (const TQString &text, const TQString &word, uint pos)
 
static TQString remrange (const TQString &text, const char *range)
 
static TQString remword (const TQString &text, uint pos)
 
static TQString remword (const TQString &text, const TQString &word)
 
static TQString capwords (const TQString &text)
 
static TQStringList capwords (const TQStringList &list)
 
static TQString reverse (const TQString &text)
 
static TQStringList reverse (const TQStringList &list)
 
static TQString ljust (const TQString &text, uint width) TDE_DEPRECATED
 
static TQString rjust (const TQString &text, uint width) TDE_DEPRECATED
 
static TQString center (const TQString &text, uint width)
 
static TQString lsqueeze (const TQString &str, uint maxlen=40)
 
static TQString lEmSqueeze (const TQString &name, const TQFontMetrics &fontMetrics, uint maxlen=30)
 
static TQString lPixelSqueeze (const TQString &name, const TQFontMetrics &fontMetrics, uint maxPixels)
 
static TQString csqueeze (const TQString &str, uint maxlen=40)
 
static TQString cEmSqueeze (const TQString &name, const TQFontMetrics &fontMetrics, uint maxlen=30)
 
static TQString cPixelSqueeze (const TQString &name, const TQFontMetrics &fontMetrics, uint maxPixels)
 
static TQString rsqueeze (const TQString &str, uint maxlen=40)
 
static TQString rEmSqueeze (const TQString &name, const TQFontMetrics &fontMetrics, uint maxlen=30)
 
static TQString rPixelSqueeze (const TQString &name, const TQFontMetrics &fontMetrics, uint maxPixels)
 
static bool matchFileName (const TQString &filename, const TQString &pattern)
 
static TQStringList perlSplit (const TQString &sep, const TQString &s, uint max=0)
 
static TQStringList perlSplit (const TQChar &sep, const TQString &s, uint max=0)
 
static TQStringList perlSplit (const TQRegExp &sep, const TQString &s, uint max=0)
 
static TQString tagURLs (const TQString &text)
 
static TQString obscure (const TQString &str)
 
static bool isUtf8 (const char *str)
 
static TQString from8Bit (const char *str)
 
static TDE_DEPRECATED bool matchFilename (const TQString &filename, const TQString &pattern)
 

Detailed Description

This class contains utility functions for handling strings.

This class is not a substitute for the TQString class. What I tried to do with this class is provide an easy way to cut/slice/splice words inside sentences in whatever order desired. While the main focus of this class are words (ie characters separated by spaces/tabs), the two core functions here ( split() and join() ) will function given any char to use as a separator. This will make it easy to redefine what a 'word' means in the future if needed.

I freely stole some of the function names from python. I also think some of these were influenced by mIRC (yes, believe it if you will, I used to write a LOT of scripts in mIRC).

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 TQString&, const char *) and remword(const TQString&, uint) functions for more detail.

This class contains no data members of its own. All strings are cut on the fly and returned as new qstrings/qstringlists.

Quick example on how to use:

KStringHandler kstr;
TQString line = "This is a test of the strings";
cout << "1> " << kstr.word( line , "4:" ) << "\n";
cout << "2> " << kstr.remrange( line , "2:5" ) << "\n";
cout << "2> " << kstr.reverse( line ) << "\n";
cout << "2> " << kstr.center( kstr.word( line , 4 ) , 15 ) << "\n";
KStringHandler
This class contains utility functions for handling strings.
Definition: kstringhandler.h:70
KStringHandler::center
static TQString center(const TQString &text, uint width)
Centers a string and returns a string at least 'width' characters wide.
Definition: kstringhandler.cpp:261
KStringHandler::word
static TQString word(const TQString &text, uint pos) TDE_DEPRECATED
Returns the nth word in the string if found Returns a EMPTY (not null) string otherwise.
Definition: kstringhandler.cpp:39
KStringHandler::remrange
static TQString remrange(const TQString &text, const char *range)
Removes a word or ranges of words from the string, and returns a new string.
Definition: kstringhandler.cpp:122
KStringHandler::reverse
static TQString reverse(const TQString &text)
Reverses the order of the words in a string "hello there" becomes "there hello" (string)
Definition: kstringhandler.cpp:219

and so forth.

Class 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

Definition at line 69 of file kstringhandler.h.

Member Function Documentation

◆ capwords() [1/2]

TQString KStringHandler::capwords ( const TQString &  text)
static

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

Parameters
textthe text to capitalize
Returns
the resulting string

Definition at line 193 of file kstringhandler.cpp.

◆ capwords() [2/2]

TQStringList KStringHandler::capwords ( const TQStringList &  list)
static

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

Parameters
listthe list to capitalize
Returns
the resulting list

Definition at line 207 of file kstringhandler.cpp.

◆ cEmSqueeze()

TQString KStringHandler::cEmSqueeze ( const TQString &  name,
const TQFontMetrics &  fontMetrics,
uint  maxlen = 30 
)
static

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

Similar to method above, except that it truncates based on pixel width rather than the number of characters

Parameters
nameis the string to modify
fontMetricsis the font metrics to use to calculate character sizes
maxlenis the maximum length in ems the modified string will have If the original string is shorter than "maxlen", it is returned verbatim
Returns
the modified string
Since
3.2

Definition at line 333 of file kstringhandler.cpp.

◆ center()

TQString KStringHandler::center ( const TQString &  text,
uint  width 
)
static

Centers a string and returns a string at least 'width' characters wide.

If the string is longer than the width, the original string is returned. It is never truncated.

Parameters
textthe text to justify
widththe desired width of the new string
Returns
the resulting string

Definition at line 261 of file kstringhandler.cpp.

◆ cPixelSqueeze()

TQString KStringHandler::cPixelSqueeze ( const TQString &  name,
const TQFontMetrics &  fontMetrics,
uint  maxPixels 
)
static

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

Similar to method above, except that maxlen is the width in pixels to truncate to

Parameters
nameis the string to modify
fontMetricsis the font metrics to use to calculate character sizes
maxPixelsis the maximum pixel length the modified string will have If the original string is shorter than "maxlen", it is returned verbatim
Returns
the modified string
Since
3.2

Definition at line 338 of file kstringhandler.cpp.

◆ csqueeze()

TQString KStringHandler::csqueeze ( const TQString &  str,
uint  maxlen = 40 
)
static

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 285 of file kstringhandler.cpp.

◆ from8Bit()

TQString KStringHandler::from8Bit ( const char *  str)
static

Construct TQString from a c string, guessing whether it is UTF8- or Local8Bit-encoded.

Parameters
strthe input string
Returns
the (hopefully correctly guessed) TQString representation of str
Since
3.2

Definition at line 652 of file kstringhandler.cpp.

◆ insword()

TQString KStringHandler::insword ( const TQString &  text,
const TQString &  word,
uint  pos 
)
static

Inserts a word into the string, and returns a new string with the word included.

the first index is zero (0). If there are not pos words in the original string, the new word will be appended to the end.

Parameters
textthe original text
wordthe word to insert
posthe position (in words) for the new word
Returns
the resulting string

Definition at line 80 of file kstringhandler.cpp.

◆ isUtf8()

bool KStringHandler::isUtf8 ( const char *  str)
static

Guess whether a string is UTF8 encoded.

Parameters
strthe string to check
Returns
true if UTF8. If false, the string is probably in Local8Bit.
Since
3.2

Definition at line 567 of file kstringhandler.cpp.

◆ lEmSqueeze()

TQString KStringHandler::lEmSqueeze ( const TQString &  name,
const TQFontMetrics &  fontMetrics,
uint  maxlen = 30 
)
static

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

Similar to method above, except that it truncates based on pixel width rather than the number of characters

Parameters
nameis the string to modify
fontMetricsis the font metrics to use to calculate character sizes
maxlenis the maximum length in ems the modified string will have If the original string is shorter than "maxlen", it is returned verbatim
Returns
the modified string
Since
3.2

Definition at line 303 of file kstringhandler.cpp.

◆ ljust()

TQString KStringHandler::ljust ( const TQString &  text,
uint  width 
)
static

Left-justifies a string and returns a string at least 'width' characters wide.

If the string is longer than the width, the original string is returned. It is never truncated.

Parameters
textthe text to justify
widththe desired width of the new string
Returns
the resulting string
Deprecated:
use TQString::leftJustify instead

Definition at line 251 of file kstringhandler.cpp.

◆ lPixelSqueeze()

TQString KStringHandler::lPixelSqueeze ( const TQString &  name,
const TQFontMetrics &  fontMetrics,
uint  maxPixels 
)
static

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

Similar to method above, except that maxlen is the width in pixels to truncate to

Parameters
nameis the string to modify
fontMetricsis the font metrics to use to calculate character sizes
maxPixelsis the maximum pixel length the modified string will have If the original string is shorter than "maxlen", it is returned verbatim
Returns
the modified string
Since
3.2

Definition at line 308 of file kstringhandler.cpp.

◆ lsqueeze()

TQString KStringHandler::lsqueeze ( const TQString &  str,
uint  maxlen = 40 
)
static

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 276 of file kstringhandler.cpp.

◆ matchFileName()

bool KStringHandler::matchFileName ( const TQString &  filename,
const TQString &  pattern 
)
static

Match a filename.

Parameters
filenameis the real decoded filename (or dirname without trailing '/').
patternis a pattern like *.txt, *.tar.gz, Makefile.*, README, etc. Patterns with two asterisks like "*.*pk" are not supported.
Returns
true if the given filename matches the given pattern

Definition at line 413 of file kstringhandler.cpp.

◆ matchFilename()

static TDE_DEPRECATED bool KStringHandler::matchFilename ( const TQString &  filename,
const TQString &  pattern 
)
inlinestatic
Deprecated:
Use matchFileName () instead.

Definition at line 435 of file kstringhandler.h.

◆ obscure()

TQString KStringHandler::obscure ( const TQString &  str)
static

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
Since
3.2

Definition at line 556 of file kstringhandler.cpp.

◆ perlSplit() [1/3]

TQStringList KStringHandler::perlSplit ( const TQChar &  sep,
const TQString &  s,
uint  max = 0 
)
static

Split a TQString into a TQStringList in a similar fashion to the static TQStringList 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(' ', "tdeparts reaches the parts other parts can't", 3)
TQStringList contains: "tdeparts", "reaches", "the parts other parts can't"
KStringHandler::perlSplit
static TQStringList perlSplit(const TQString &sep, const TQString &s, uint max=0)
Split a TQString into a TQStringList in a similar fashion to the static TQStringList function in Qt,...
Definition: kstringhandler.cpp:452
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 TQStringList containing tokens extracted from s.

Definition at line 478 of file kstringhandler.cpp.

◆ perlSplit() [2/3]

TQStringList KStringHandler::perlSplit ( const TQRegExp &  sep,
const TQString &  s,
uint  max = 0 
)
static

Split a TQString into a TQStringList in a similar fashion to the static TQStringList 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(TQRegExp("[! ]", "Split me up ! I'm bored ! OK ?", 3)
TQStringList 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 TQStringList containing tokens extracted from s.

Definition at line 504 of file kstringhandler.cpp.

◆ perlSplit() [3/3]

TQStringList KStringHandler::perlSplit ( const TQString &  sep,
const TQString &  s,
uint  max = 0 
)
static

Split a TQString into a TQStringList in a similar fashion to the static TQStringList 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)
TQStringList 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 TQStringList containing tokens extracted from s.

Definition at line 452 of file kstringhandler.cpp.

◆ remrange()

TQString KStringHandler::remrange ( const TQString &  text,
const char *  range 
)
static

Removes a word or ranges of words from the string, and returns a new string.

The ranges definitions follow the definitions for the word() function.

  • "0" removes the very first word
  • "0:" removes the first the the last word
  • "0:3" removes the first to fourth words
  • ":3" removes everything up to the fourth word
    Parameters
    textthe original text
    rangethe words to remove (see description)
    Returns
    the resulting string

Definition at line 122 of file kstringhandler.cpp.

◆ rEmSqueeze()

TQString KStringHandler::rEmSqueeze ( const TQString &  name,
const TQFontMetrics &  fontMetrics,
uint  maxlen = 30 
)
static

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

Similar to method above, except that it truncates based on pixel width rather than the number of characters

Parameters
nameis the string to modify
fontMetricsis the font metrics to use to calculate character sizes
maxlenis the maximum length in ems the modified string will have If the original string is shorter than "maxlen", it is returned verbatim
Returns
the modified string
Since
3.2

Definition at line 380 of file kstringhandler.cpp.

◆ remword() [1/2]

TQString KStringHandler::remword ( const TQString &  text,
const TQString &  word 
)
static

Removes a matching word from the string, and returns a new string.

Note that only ONE match is removed.

Parameters
textthe original text
wordthe word to remove
Returns
the resulting string

Definition at line 168 of file kstringhandler.cpp.

◆ remword() [2/2]

TQString KStringHandler::remword ( const TQString &  text,
uint  pos 
)
static

Removes a word at the given index, and returns a new string.

The first index is zero (0).

Parameters
textthe original text
posthe position (in words) of thw word to delete
Returns
the resulting string

Definition at line 151 of file kstringhandler.cpp.

◆ reverse() [1/2]

TQString KStringHandler::reverse ( const TQString &  text)
static

Reverses the order of the words in a string "hello there" becomes "there hello" (string)

Parameters
textthe text to reverse
Returns
the resulting string

Definition at line 219 of file kstringhandler.cpp.

◆ reverse() [2/2]

TQStringList KStringHandler::reverse ( const TQStringList &  list)
static

Reverses the order of the words in a list [hello, there] becomes [there, hello] (list)

Parameters
listthe list to reverse
Returns
the resulting list

Definition at line 233 of file kstringhandler.cpp.

◆ rjust()

TQString KStringHandler::rjust ( const TQString &  text,
uint  width 
)
static

Right-justifies a string and returns a string at least 'width' characters wide.

If the string is longer than the width, the original string is returned. It is never truncated.

Parameters
textthe text to justify
widththe desired width of the new string
Returns
the resulting string
Deprecated:
use TQString::rightJustify instead

Definition at line 256 of file kstringhandler.cpp.

◆ rPixelSqueeze()

TQString KStringHandler::rPixelSqueeze ( const TQString &  name,
const TQFontMetrics &  fontMetrics,
uint  maxPixels 
)
static

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

Similar to method above, except that maxlen is the width in pixels to truncate to

Parameters
nameis the string to modify
fontMetricsis the font metrics to use to calculate character sizes
maxPixelsis the maximum pixel length the modified string will have If the original string is shorter than "maxlen", it is returned verbatim
Returns
the modified string
Since
3.2

Definition at line 385 of file kstringhandler.cpp.

◆ rsqueeze()

TQString KStringHandler::rsqueeze ( const TQString &  str,
uint  maxlen = 40 
)
static

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 294 of file kstringhandler.cpp.

◆ setword()

TQString KStringHandler::setword ( const TQString &  text,
const TQString &  word,
uint  pos 
)
static

Replaces a word in the string, and returns a new string with the word included.

the first index is zero (0). If there are not pos words in the original string, the new word will be appended to the end.

Parameters
textthe original text
wordthe word to insert
posthe position (in words) for the new word
Returns
the resulting string

Definition at line 100 of file kstringhandler.cpp.

◆ tagURLs()

TQString KStringHandler::tagURLs ( const TQString &  text)
static

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

Parameters
textthe string which may contain URLs
Returns
the resulting text
Since
3.1

Definition at line 531 of file kstringhandler.cpp.

◆ word() [1/2]

TQString KStringHandler::word ( const TQString &  text,
const char *  range 
)
static

Returns a range of words from that string.

Ie:

  • "0" returns the very first word
  • "0:" returns the first to the last word
  • "0:3" returns the first to fourth words
  • ":3" returns everything up to the fourth word

If you grok python, you're set.

Parameters
textthe string to search for the words
rangethe words to return (see description)
Returns
the words, or an empty string if not found

Definition at line 44 of file kstringhandler.cpp.

◆ word() [2/2]

TQString KStringHandler::word ( const TQString &  text,
uint  pos 
)
static

Returns the nth word in the string if found Returns a EMPTY (not null) string otherwise.

Note that the FIRST index is 0.

Parameters
textthe string to search for the words
posthe position of the word to search
Returns
the word, or an empty string if not found
Deprecated:
use TQString::section instead

Definition at line 39 of file kstringhandler.cpp.


The documentation for this class was generated from the following files:
  • kstringhandler.h
  • kstringhandler.cpp

tdecore

Skip menu "tdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.4
This website is maintained by Timothy Pearson.