Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
Public Member Functions | Static Public Member Functions | Static Public Attributes

String Class Reference
[Datastructures]

A more convenient string class. More...

#include <OpenMS/DATASTRUCTURES/String.h>

Inherits std::string.

List of all members.

Public Types

Type definitions

enum  QuotingMethod { NONE, ESCAPE, DOUBLE }
 

How to handle embedded quotes when quoting strings.

More...
typedef iterator Iterator
 Iterator.
typedef const_iterator ConstIterator
 Const Iterator.
typedef reverse_iterator ReverseIterator
 Reverse Iterator.
typedef const_reverse_iterator ConstReverseIterator
 Const reverse Iterator.
typedef size_type SizeType
 UInt type.

Public Member Functions

bool split (const char splitter, std::vector< String > &substrings, bool quote_protect=false) const
 Splits a string into substrings using splitter as delimiter.
bool split (const String &splitter, std::vector< String > &substrings) const
 Splits a string into substrings using splitter (the whole string) as delimiter.
bool split_quoted (const String &splitter, std::vector< String > &substrings, char q= '"', QuotingMethod method = ESCAPE) const
 Splits a string into substrings using splitter (the whole string) as delimiter, but does not split within quoted substrings.
template<class StringIterator >
void concatenate (StringIterator first, StringIterator last, const String &glue="")
 Concatenates all elements from first to last-1 and inserts glue between the elements.
Constructors

 String ()
 Default constructor.
 String (const std::string &s)
 Constructor from std::string.
 String (const QString &s)
 Constructor from Qt QString.
 String (const char *s)
 Constructor from char*.
 String (const char c)
 Constructor from a char.
 String (const char *s, SizeType length)
 Constructor from char* (only length chracters).
 String (size_t len, char c)
 Constructor from char (repeats the char len times).
template<class InputIterator >
 String (InputIterator first, InputIterator last)
 Constructor from a char range.
 String (int i)
 Constructor from an integer.
 String (unsigned int i)
 Constructor from an unsigned integer.
 String (short int i)
 Constructor from an integer.
 String (short unsigned int i)
 Constructor from an unsigned integer.
 String (long int i)
 Constructor from an integer.
 String (long unsigned int i)
 Constructor from an unsigned integer.
 String (long long unsigned int i)
 Constructor from an unsigned integer.
 String (long long signed int i)
 Constructor from an unsigned integer.
 String (float f)
 Constructor from float.
 String (double d)
 Constructor from double.
 String (long double ld)
 Constructor from long double.
 String (const DataValue &d)
 Constructor from DataValue (casted to String).
Predicates

bool hasPrefix (const String &string) const
 true if String begins with string, false otherwise
bool hasSuffix (const String &string) const
 true if String ends with string, false otherwise
bool hasSubstring (const String &string) const
 true if String contains the string, false otherwise
bool has (Byte byte) const
 true if String contains the byte, false otherwise
Accessors

String prefix (SizeType length) const
 returns the prefix of length length
String suffix (SizeType length) const
 returns the suffix of length length
String prefix (Int length) const
 returns the prefix of length length
String suffix (Int length) const
 returns the suffix of length length
String prefix (char delim) const
 returns the prefix up to the first occurence of char delim (excluding it)
String suffix (char delim) const
 returns the suffix up to the last occurence of char delim (excluding it)
String substr (size_t pos=0, size_t n=npos) const
 Wrapper for the STL substr() method. Returns a String object with its contents initialized to a substring of the current object.
String chop (Size n) const
 Returns a substring where n characters were removed from the end of the string.
Mutators

All these methods return a reference to the string in order to make them chainable

Stringreverse ()
 inverts the direction of the string
Stringtrim ()
 removes whitespaces (space, tab, line feed, carriage return) at the beginning and the end of the string
Stringquote (char q= '"', QuotingMethod method = ESCAPE)
 Wraps the string in quotation marks.
Stringunquote (char q= '"', QuotingMethod method = ESCAPE)
 Reverses changes made by the quote method.
Stringsimplify ()
 merges subsequent whitespaces to one blank character
StringfillLeft (char c, UInt size)
 Adds c on the left side until the size of the string is size.
StringfillRight (char c, UInt size)
 Adds c on the right side until the size of the string is size.
StringtoUpper ()
 Converts the string to uppercase.
StringtoLower ()
 Converts the string to lowercase.
StringfirstToUpper ()
 Converts the first letter of the string to uppercase.
Stringsubstitute (char from, char to)
 Replaces all occurences of the character from by the character to.
Stringsubstitute (const String &from, const String &to)
 Replaces all occurences of the string from by the string to.
Stringremove (char what)
 Remove all occurences of the character what.
StringensureLastChar (char end)
 Makes sure the string ends with the character end.
StringremoveWhitespaces ()
 removes whitespaces (space, tab, line feed, carriage return)
Converters

Int toInt () const
 Conversion to int.
Real toFloat () const
 Conversion to float.
DoubleReal toDouble () const
 Conversion to double.
QString toQString () const
 Conversion to Qt QString.
Sum operator overloads

String operator+ (int i) const
 Sum operator for an integer.
String operator+ (unsigned int i) const
 Sum operator for an unsigned integer.
String operator+ (short int i) const
 Sum operator for an integer.
String operator+ (short unsigned int i) const
 Sum operator for an unsigned integer.
String operator+ (long int i) const
 Sum operator for an integer.
String operator+ (long unsigned int i) const
 Sum operator for an unsigned integer.
String operator+ (long long unsigned int i) const
 Sum operator for an unsigned integer.
String operator+ (float f) const
 Sum operator for float.
String operator+ (double d) const
 Sum operator for double.
String operator+ (long double ld) const
 Sum operator for long double.
String operator+ (char c) const
 Sum operator for char.
String operator+ (const char *s) const
 Sum operator for char*.
String operator+ (const String &s) const
 Sum operatr for String.
String operator+ (const std::string &s) const
 Sum operator for std::string.
Append operator overloads

Stringoperator+= (int i)
 Sum operator for an integer.
Stringoperator+= (unsigned int i)
 Sum operator for an unsigned integer.
Stringoperator+= (short int i)
 Sum operator for an integer.
Stringoperator+= (short unsigned int i)
 Sum operator for an unsigned integer.
Stringoperator+= (long int i)
 Sum operator for an integer.
Stringoperator+= (long unsigned int i)
 Sum operator for an unsigned integer.
Stringoperator+= (long long unsigned int i)
 Sum operator for an unsigned integer.
Stringoperator+= (float f)
 Sum operator for float.
Stringoperator+= (double d)
 Sum operator for double.
Stringoperator+= (long double d)
 Sum operator for long double.
Stringoperator+= (char c)
 Sum operator for char.
Stringoperator+= (const char *s)
 Sum operator for char*.
Stringoperator+= (const String &s)
 Sum operatr for String.
Stringoperator+= (const std::string &s)
 Sum operator for std::string.

Static Public Member Functions

static String random (UInt length)
 returns a random string of the given length. It consists of [0-9a-zA-Z]
static String number (DoubleReal d, UInt n)
 returns a string for d with exactly n decimal places
static String numberLength (DoubleReal d, UInt n)
 Returns a string with at maximum n characters for d.

Static Public Attributes

static const String EMPTY
 Empty string for comparisons.

Detailed Description

A more convenient string class.

It is based on std::string but adds a lot of methods for convenience.


Member Typedef Documentation

typedef const_iterator ConstIterator

Const Iterator.

typedef const_reverse_iterator ConstReverseIterator

Const reverse Iterator.

typedef iterator Iterator

Iterator.

typedef reverse_iterator ReverseIterator

Reverse Iterator.

typedef size_type SizeType

UInt type.


Member Enumeration Documentation

How to handle embedded quotes when quoting strings.

Enumerator:
NONE 
ESCAPE 
DOUBLE 

Constructor & Destructor Documentation

String (  ) 

Default constructor.

String ( const std::string &  s  ) 

Constructor from std::string.

String ( const QString &  s  ) 

Constructor from Qt QString.

String ( const char *  s  ) 

Constructor from char*.

String ( const char  c  ) 

Constructor from a char.

String ( const char *  s,
SizeType  length 
)

Constructor from char* (only length chracters).

String ( size_t  len,
char  c 
)

Constructor from char (repeats the char len times).

String ( InputIterator  first,
InputIterator  last 
) [inline]

Constructor from a char range.

String ( int  i  ) 

Constructor from an integer.

String ( unsigned int  i  ) 

Constructor from an unsigned integer.

String ( short int  i  ) 

Constructor from an integer.

String ( short unsigned int  i  ) 

Constructor from an unsigned integer.

String ( long int  i  ) 

Constructor from an integer.

String ( long unsigned int  i  ) 

Constructor from an unsigned integer.

String ( long long unsigned int  i  ) 

Constructor from an unsigned integer.

String ( long long signed int  i  ) 

Constructor from an unsigned integer.

String ( float  f  ) 

Constructor from float.

String ( double  d  ) 

Constructor from double.

String ( long double  ld  ) 

Constructor from long double.

String ( const DataValue d  ) 

Constructor from DataValue (casted to String).


Member Function Documentation

String chop ( Size  n  )  const

Returns a substring where n characters were removed from the end of the string.

If n is greater than size(), the result is an empty string.

Parameters:
n Number of characters that will be removed from the end of the string.
void concatenate ( StringIterator  first,
StringIterator  last,
const String glue = "" 
) [inline]

Concatenates all elements from first to last-1 and inserts glue between the elements.

See also:
split().

References String::operator+=().

String& ensureLastChar ( char  end  ) 

Makes sure the string ends with the character end.

String& fillLeft ( char  c,
UInt  size 
)

Adds c on the left side until the size of the string is size.

String& fillRight ( char  c,
UInt  size 
)

Adds c on the right side until the size of the string is size.

String& firstToUpper (  ) 

Converts the first letter of the string to uppercase.

bool has ( Byte  byte  )  const

true if String contains the byte, false otherwise

Referenced by DTAFile::load(), DTA2DFile::load(), TOPPRNPxl::main_(), and MzXMLHandler< MapType >::startElement().

bool hasPrefix ( const String string  )  const
bool hasSubstring ( const String string  )  const

true if String contains the string, false otherwise

Referenced by MzTabModification::fromCellString(), main(), and MzXMLHandler< MapType >::writeTo().

bool hasSuffix ( const String string  )  const

true if String ends with string, false otherwise

Referenced by XMLHandler::expectList_(), XMassFile::load(), and TOPPViewBase::showCurrentPeaksAs2D().

static String number ( DoubleReal  d,
UInt  n 
) [static]
static String numberLength ( DoubleReal  d,
UInt  n 
) [static]

Returns a string with at maximum n characters for d.

If d is larger, scientific notation is used.

String operator+ ( int  i  )  const

Sum operator for an integer.

String operator+ ( unsigned int  i  )  const

Sum operator for an unsigned integer.

String operator+ ( short int  i  )  const

Sum operator for an integer.

String operator+ ( short unsigned int  i  )  const

Sum operator for an unsigned integer.

String operator+ ( long int  i  )  const

Sum operator for an integer.

String operator+ ( long unsigned int  i  )  const

Sum operator for an unsigned integer.

String operator+ ( long long unsigned int  i  )  const

Sum operator for an unsigned integer.

String operator+ ( float  f  )  const

Sum operator for float.

String operator+ ( double  d  )  const

Sum operator for double.

String operator+ ( long double  ld  )  const

Sum operator for long double.

String operator+ ( char  c  )  const

Sum operator for char.

String operator+ ( const char *  s  )  const

Sum operator for char*.

String operator+ ( const String s  )  const

Sum operatr for String.

String operator+ ( const std::string &  s  )  const

Sum operator for std::string.

String& operator+= ( long double  d  ) 

Sum operator for long double.

String& operator+= ( unsigned int  i  ) 

Sum operator for an unsigned integer.

String& operator+= ( int  i  ) 

Sum operator for an integer.

Referenced by String::concatenate().

String& operator+= ( short int  i  ) 

Sum operator for an integer.

String& operator+= ( short unsigned int  i  ) 

Sum operator for an unsigned integer.

String& operator+= ( long int  i  ) 

Sum operator for an integer.

String& operator+= ( long unsigned int  i  ) 

Sum operator for an unsigned integer.

String& operator+= ( long long unsigned int  i  ) 

Sum operator for an unsigned integer.

String& operator+= ( float  f  ) 

Sum operator for float.

String& operator+= ( double  d  ) 

Sum operator for double.

String& operator+= ( char  c  ) 

Sum operator for char.

String& operator+= ( const char *  s  ) 

Sum operator for char*.

String& operator+= ( const String s  ) 

Sum operatr for String.

String& operator+= ( const std::string &  s  ) 

Sum operator for std::string.

String prefix ( char  delim  )  const

returns the prefix up to the first occurence of char delim (excluding it)

Exceptions:
Exception::ElementNotFound is thrown if delim is not found
String prefix ( SizeType  length  )  const
String prefix ( Int  length  )  const

returns the prefix of length length

Exceptions:
Exception::IndexUnderflow is thrown if length is smaller than zero
Exception::IndexOverflow is thrown if length is bigger than the size
String& quote ( char  q = '"',
QuotingMethod  method = ESCAPE 
)

Wraps the string in quotation marks.

The quotation mark can be specified by parameter q (typically single or double quote); embedded quotation marks are handled according to method by backslash-escaping, doubling, or not at all.

See also:
unquote()
static String random ( UInt  length  )  [static]

returns a random string of the given length. It consists of [0-9a-zA-Z]

String& remove ( char  what  ) 

Remove all occurences of the character what.

Referenced by MzTabModification::fromCellString().

String& removeWhitespaces (  ) 

removes whitespaces (space, tab, line feed, carriage return)

Referenced by MzXMLHandler< MapType >::endElement().

String& reverse (  ) 

inverts the direction of the string

String& simplify (  ) 

merges subsequent whitespaces to one blank character

Referenced by MS2File::load().

bool split ( const char  splitter,
std::vector< String > &  substrings,
bool  quote_protect = false 
) const

Splits a string into substrings using splitter as delimiter.

If splitter is not found, the whole string is put into substrings. If splitter is empty, the string is split into individual characters. If the invoking string is empty, substrings will also be empty.

quote_protect (default: false) can be used to split only between quoted blocks e.g. ' "a string" , "another string with , in it" ' results in only two substrings (with double quotation marks removed). Every returned substring is trimmed and then (if present) has surrounding quotation marks removed.

Returns:
true if one or more splits occurred, false otherwise
See also:
concatenate().

Referenced by MzTabSpectraRef::fromCellString(), MzTabModificationList::fromCellString(), MzTabModification::fromCellString(), MzTabStringList::fromCellString(), MzTabParameterList::fromCellString(), MzTabParameter::fromCellString(), MzTabDoubleList::fromCellString(), initModificationMassesRNA(), SpecArrayFile::load(), MsInspectFile::load(), MS2File::load(), KroenikFile::load(), DTAFile::load(), DTA2DFile::load(), and FeatureFinderAlgorithmIsotopeWavelet< PeakType, FeatureType >::updateMembers_().

bool split ( const String splitter,
std::vector< String > &  substrings 
) const

Splits a string into substrings using splitter (the whole string) as delimiter.

If splitter is not found, the whole string is put into substrings. If splitter is empty, the string is split into individual characters. If the invoking string is empty, substrings will also be empty.

Returns:
true if one or more splits occurred, false otherwise
See also:
concatenate().
bool split_quoted ( const String splitter,
std::vector< String > &  substrings,
char  q = '"',
QuotingMethod  method = ESCAPE 
) const

Splits a string into substrings using splitter (the whole string) as delimiter, but does not split within quoted substrings.

A "quoted substring" has the format as produced by quote(q, method), where q is the quoting character and method defines the handling of embedded quotes. Substrings will not be "unquoted" or otherwise processed.

If splitter is not found, the whole string is put into substrings. If splitter or the invoking string is empty, substrings will also be empty.

Returns:
true if one or more splits occurred, false otherwise
Exceptions:
Exception::ConversionError is thrown if quotation marks are not balanced
See also:
concatenate(), quote().
String& substitute ( char  from,
char  to 
)

Replaces all occurences of the character from by the character to.

Referenced by initModificationMassesRNA(), TOPPRNPxl::main_(), MzMLHandler< MapType >::writeCV_(), and MzMLHandler< MapType >::writeUserParam_().

String& substitute ( const String from,
const String to 
)

Replaces all occurences of the string from by the string to.

String substr ( size_t  pos = 0,
size_t  n = npos 
) const

Wrapper for the STL substr() method. Returns a String object with its contents initialized to a substring of the current object.

Parameters:
pos Position of a character in the current string object to be used as starting character for the substring. If the pos is past the end of the string, it is set to the end of the string.
n Length of the substring. If this value would make the substring to span past the end of the current string content, only those characters until the end of the string are used. npos is a static member constant value with the greatest possible value for an element of type size_t, therefore, when this value is used, all the characters between pos and the end of the string are used as the initialization substring.

Referenced by XMLHandler::asDateTime_(), XMLHandler::attributeAsDoubleList_(), XMLHandler::attributeAsIntList_(), XMLHandler::attributeAsStringList_(), FeatureFinderAlgorithmSH< PeakType, FeatureType >::getNativeScanId(), DTA2DFile::load(), notInSeq(), MzXMLHandler< MapType >::writeTo(), and MzDataHandler< MapType >::writeTo().

String suffix ( char  delim  )  const

returns the suffix up to the last occurence of char delim (excluding it)

Exceptions:
Exception::ElementNotFound is thrown if delim is not found
String suffix ( Int  length  )  const

returns the suffix of length length

Exceptions:
Exception::IndexUnderflow is thrown if length is smaller than zero
Exception::IndexOverflowis thrown if length is bigger than the size
String suffix ( SizeType  length  )  const

returns the suffix of length length

Exceptions:
Exception::IndexOverflow is thrown if length is bigger than the size

Referenced by XMassFile::load(), and MzXMLHandler< MapType >::startElement().

DoubleReal toDouble (  )  const
Real toFloat (  )  const

Conversion to float.

Exceptions:
Exception::ConversionError is thrown if the string could not be converted to float

Referenced by XMLHandler::asFloat_().

Int toInt (  )  const

Conversion to int.

This method extracts only the integral part of the string. If you want the result rounded, use toFloat() and round the result.

Exceptions:
Exception::ConversionError is thrown if the string could not be converted to int

Referenced by XMLHandler::asInt_(), XMLHandler::asUInt_(), MzTabModification::fromCellString(), MzTabInteger::fromCellString(), FeatureFinderAlgorithmSH< PeakType, FeatureType >::getNativeScanId(), MzMLHandler< MapType >::handleCVParam_(), MzMLHandler< MapType >::handleUserParam_(), MzXMLHandler< MapType >::writeTo(), and MzDataHandler< MapType >::writeTo().

String& toLower (  ) 

Converts the string to lowercase.

Referenced by MzXMLHandler< MapType >::writeTo().

QString toQString (  )  const
String& toUpper (  ) 
String& trim (  ) 
String& unquote ( char  q = '"',
QuotingMethod  method = ESCAPE 
)

Reverses changes made by the quote method.

Removes surrounding quotation marks (given by parameter q); handles embedded quotes according to method.

Exceptions:
Exception::ConversionError is thrown if the string does not have the format produced by quote
See also:
quote()

Member Data Documentation

const String EMPTY [static]

Empty string for comparisons.


OpenMS / TOPP release 1.10.0 Documentation generated on Thu Mar 7 2013 09:42:53 using doxygen 1.7.1