OpenMS
String Class Reference

A more convenient string class. More...

#include <OpenMS/DATASTRUCTURES/String.h>

Inheritance diagram for String:
[legend]
Collaboration diagram for String:
[legend]

Public Types

Type definitions
enum  QuotingMethod { NONE , ESCAPE , DOUBLE }
 How to handle embedded quotes when quoting strings. More...
 
typedef iterator Iterator
 Iterator. More...
 
typedef const_iterator ConstIterator
 Const Iterator. More...
 
typedef reverse_iterator ReverseIterator
 Reverse Iterator. More...
 
typedef const_reverse_iterator ConstReverseIterator
 Const reverse Iterator. More...
 
typedef size_type SizeType
 UInt type. More...
 

Public Member Functions

Constructors
 String ()
 Default constructor. More...
 
 String (const String &)=default
 Copy constructor. More...
 
 String (String &&)=default
 Move constructor. More...
 
 String (const std::string &s)
 Constructor from std::string. More...
 
 String (const std::string_view &sv)
 Constructor from std::string_view. More...
 
 String (const QString &s)
 Constructor from Qt QString. More...
 
 String (const char *s)
 Constructor from char*. More...
 
 String (const char c)
 Constructor from a char. More...
 
 String (const char *s, SizeType length)
 Constructor from char* (only length characters) More...
 
 String (size_t len, char c)
 Constructor from char (repeats the char len times) More...
 
template<class InputIterator >
 String (InputIterator first, InputIterator last)
 Constructor from a char range. More...
 
 String (int i)
 Constructor from an integer. More...
 
 String (unsigned int i)
 Constructor from an unsigned integer. More...
 
 String (short int i)
 Constructor from an integer. More...
 
 String (short unsigned int i)
 Constructor from an unsigned integer. More...
 
 String (long int i)
 Constructor from an integer. More...
 
 String (long unsigned int i)
 Constructor from an unsigned integer. More...
 
 String (long long unsigned int i)
 Constructor from an unsigned integer. More...
 
 String (long long signed int i)
 Constructor from an unsigned integer. More...
 
 String (float f, bool full_precision=true)
 Constructor from float (full_precision controls number of fractional digits, 3 digits when false, and 6 when true) More...
 
 String (double d, bool full_precision=true)
 Constructor from double (full_precision controls number of fractional digits, 3 digits when false, and 15 when true) More...
 
 String (long double ld, bool full_precision=true)
 Constructor from long double (full_precision controls number of fractional digits, 3 digits when false, and 15 when true) More...
 
 String (const DataValue &d, bool full_precision=true)
 Constructor from DataValue (full_precision controls number of fractional digits for all double types or lists of double, 3 digits when false, and 15 when true) More...
 
Predicates
bool hasPrefix (const String &string) const
 true if String begins with string, false otherwise More...
 
bool hasSuffix (const String &string) const
 true if String ends with string, false otherwise More...
 
bool hasSubstring (const String &string) const
 true if String contains the string, false otherwise More...
 
bool has (Byte byte) const
 true if String contains the byte, false otherwise More...
 
Stringoperator= (const String &)=default
 Assignment operator. More...
 
Stringoperator= (String &&) &=default
 Move assignment operator. More...
 
Accessors
String prefix (SizeType length) const
 returns the prefix of length length More...
 
String suffix (SizeType length) const
 returns the suffix of length length More...
 
String prefix (Int length) const
 returns the prefix of length length More...
 
String suffix (Int length) const
 returns the suffix of length length More...
 
String prefix (char delim) const
 returns the prefix up to the first occurrence of char delim (excluding it) More...
 
String suffix (char delim) const
 returns the suffix up to the last occurrence of char delim (excluding it) More...
 
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. More...
 
String chop (Size n) const
 Returns a substring where n characters were removed from the end of the string. More...
 
Mutators

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

Stringreverse ()
 inverts the direction of the string More...
 
Stringtrim ()
 removes whitespaces (space, tab, line feed, carriage return) at the beginning and the end of the string More...
 
bool isQuoted (char q='"')
 Checks if the string is wrapped in quotation marks. More...
 
Stringquote (char q='"', QuotingMethod method = ESCAPE)
 Wraps the string in quotation marks. More...
 
Stringunquote (char q='"', QuotingMethod method = ESCAPE)
 Reverses changes made by the quote method. More...
 
Stringsimplify ()
 merges subsequent whitespaces to one blank character More...
 
StringfillLeft (char c, UInt size)
 Adds c on the left side until the size of the string is size. More...
 
StringfillRight (char c, UInt size)
 Adds c on the right side until the size of the string is size. More...
 
StringtoUpper ()
 Converts the string to uppercase. More...
 
StringtoLower ()
 Converts the string to lowercase. More...
 
StringfirstToUpper ()
 Converts the first letter of the string to uppercase. More...
 
Stringsubstitute (char from, char to)
 Replaces all occurrences of the character from by the character to. More...
 
Stringsubstitute (const String &from, const String &to)
 Replaces all occurrences of the string from by the string to. More...
 
Stringremove (char what)
 Remove all occurrences of the character what. More...
 
StringensureLastChar (char end)
 Makes sure the string ends with the character end. More...
 
StringremoveWhitespaces ()
 removes whitespaces (space, tab, line feed, carriage return) More...
 
Converters
Int toInt () const
 Conversion to Int. More...
 
Int32 toInt32 () const
 Conversion to Int32. More...
 
Int64 toInt64 () const
 Conversion to Int64. More...
 
float toFloat () const
 Conversion to float. More...
 
double toDouble () const
 Conversion to double. More...
 
QString toQString () const
 Conversion to Qt QString. More...
 
Sum operator overloads
String operator+ (int i) const
 Sum operator for an integer. More...
 
String operator+ (unsigned int i) const
 Sum operator for an unsigned integer. More...
 
String operator+ (short int i) const
 Sum operator for an integer. More...
 
String operator+ (short unsigned int i) const
 Sum operator for an unsigned integer. More...
 
String operator+ (long int i) const
 Sum operator for an integer. More...
 
String operator+ (long unsigned int i) const
 Sum operator for an unsigned integer. More...
 
String operator+ (long long unsigned int i) const
 Sum operator for an unsigned integer. More...
 
String operator+ (float f) const
 Sum operator for float. More...
 
String operator+ (double d) const
 Sum operator for double. More...
 
String operator+ (long double ld) const
 Sum operator for long double. More...
 
String operator+ (char c) const
 Sum operator for char. More...
 
String operator+ (const char *s) const
 Sum operator for char*. More...
 
String operator+ (const String &s) const
 Sum operator for String. More...
 
String operator+ (const std::string &s) const
 Sum operator for std::string. More...
 

Static Public Attributes

static const String EMPTY
 Empty string for comparisons. More...
 

Append operator overloads

Stringoperator+= (int i)
 Sum operator for an integer. More...
 
Stringoperator+= (unsigned int i)
 Sum operator for an unsigned integer. More...
 
Stringoperator+= (short int i)
 Sum operator for an integer. More...
 
Stringoperator+= (short unsigned int i)
 Sum operator for an unsigned integer. More...
 
Stringoperator+= (long int i)
 Sum operator for an integer. More...
 
Stringoperator+= (long unsigned int i)
 Sum operator for an unsigned integer. More...
 
Stringoperator+= (long long unsigned int i)
 Sum operator for an unsigned integer. More...
 
Stringoperator+= (float f)
 Sum operator for float. More...
 
Stringoperator+= (double d)
 Sum operator for double. More...
 
Stringoperator+= (long double d)
 Sum operator for long double. More...
 
Stringoperator+= (char c)
 Sum operator for char. More...
 
Stringoperator+= (const char *s)
 Sum operator for char*. More...
 
Stringoperator+= (const String &s)
 Sum operator for String. More...
 
Stringoperator+= (const std::string &s)
 Sum operator for std::string. More...
 
bool split (const char splitter, std::vector< String > &substrings, bool quote_protect=false) const
 Splits a string into substrings using splitter as delimiter. More...
 
bool split (const String &splitter, std::vector< String > &substrings) const
 Splits a string into substrings using splitter (the whole string) as delimiter. More...
 
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. More...
 
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. More...
 
static String random (UInt length)
 returns a random string of the given length. It consists of [0-9a-zA-Z] More...
 
static String number (double d, UInt n)
 returns a string for d with exactly n decimal places More...
 
static String numberLength (double d, UInt n)
 Returns a string with at maximum n characters for d. More...
 

Detailed Description

A more convenient string class.

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

Member Typedef Documentation

◆ ConstIterator

typedef const_iterator ConstIterator

Const Iterator.

◆ ConstReverseIterator

typedef const_reverse_iterator ConstReverseIterator

Const reverse Iterator.

◆ Iterator

typedef iterator Iterator

◆ ReverseIterator

typedef reverse_iterator ReverseIterator

Reverse Iterator.

◆ SizeType

typedef size_type SizeType

UInt type.

Member Enumeration Documentation

◆ QuotingMethod

How to handle embedded quotes when quoting strings.

Enumerator
NONE 
ESCAPE 
DOUBLE 

Constructor & Destructor Documentation

◆ String() [1/23]

String ( )

Default constructor.

◆ String() [2/23]

String ( const String )
default

Copy constructor.

◆ String() [3/23]

String ( String &&  )
default

Move constructor.

◆ String() [4/23]

String ( const std::string &  s)

Constructor from std::string.

◆ String() [5/23]

String ( const std::string_view &  sv)

Constructor from std::string_view.

◆ String() [6/23]

String ( const QString &  s)

Constructor from Qt QString.

◆ String() [7/23]

String ( const char *  s)

Constructor from char*.

◆ String() [8/23]

String ( const char  c)

Constructor from a char.

◆ String() [9/23]

String ( const char *  s,
SizeType  length 
)

Constructor from char* (only length characters)

◆ String() [10/23]

String ( size_t  len,
char  c 
)

Constructor from char (repeats the char len times)

◆ String() [11/23]

String ( InputIterator  first,
InputIterator  last 
)
inline

Constructor from a char range.

◆ String() [12/23]

String ( int  i)

Constructor from an integer.

◆ String() [13/23]

String ( unsigned int  i)

Constructor from an unsigned integer.

◆ String() [14/23]

String ( short int  i)

Constructor from an integer.

◆ String() [15/23]

String ( short unsigned int  i)

Constructor from an unsigned integer.

◆ String() [16/23]

String ( long int  i)

Constructor from an integer.

◆ String() [17/23]

String ( long unsigned int  i)

Constructor from an unsigned integer.

◆ String() [18/23]

String ( long long unsigned int  i)

Constructor from an unsigned integer.

◆ String() [19/23]

String ( long long signed int  i)

Constructor from an unsigned integer.

◆ String() [20/23]

String ( float  f,
bool  full_precision = true 
)

Constructor from float (full_precision controls number of fractional digits, 3 digits when false, and 6 when true)

◆ String() [21/23]

String ( double  d,
bool  full_precision = true 
)

Constructor from double (full_precision controls number of fractional digits, 3 digits when false, and 15 when true)

◆ String() [22/23]

String ( long double  ld,
bool  full_precision = true 
)

Constructor from long double (full_precision controls number of fractional digits, 3 digits when false, and 15 when true)

◆ String() [23/23]

String ( const DataValue d,
bool  full_precision = true 
)

Constructor from DataValue (full_precision controls number of fractional digits for all double types or lists of double, 3 digits when false, and 15 when true)

Member Function Documentation

◆ chop()

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
nNumber of characters that will be removed from the end of the string.

Referenced by IDScoreSwitcherAlgorithm::switchToGeneralScoreType().

◆ concatenate()

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().

Referenced by OpenMS::StringUtils::substitute().

◆ ensureLastChar()

String& ensureLastChar ( char  end)

Makes sure the string ends with the character end.

◆ fillLeft()

String& fillLeft ( char  c,
UInt  size 
)

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

◆ fillRight()

String& fillRight ( char  c,
UInt  size 
)

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

◆ firstToUpper()

String& firstToUpper ( )

Converts the first letter of the string to uppercase.

◆ has()

bool has ( Byte  byte) const

true if String contains the byte, false otherwise

Referenced by DTA2DFile::load(), DTAFile::load(), and XMLHandler::writeXMLEscape().

◆ hasPrefix()

bool hasPrefix ( const String string) const

true if String begins with string, false otherwise

Referenced by XMLHandler::expectList_(), MascotGenericFile::getNextSpectrum_(), DTA2DFile::load(), and XMassFile::load().

◆ hasSubstring()

bool hasSubstring ( const String string) const

◆ hasSuffix()

◆ isQuoted()

bool isQuoted ( char  q = '"')

Checks if the string is wrapped in quotation marks.

The quotation mark can be specified by parameter q (typically single or double quote).

See also
unquote()

◆ number()

static String number ( double  d,
UInt  n 
)
static

returns a string for d with exactly n decimal places

◆ numberLength()

static String numberLength ( double  d,
UInt  n 
)
static

Returns a string with at maximum n characters for d.

If d is larger, scientific notation is used.

◆ operator+() [1/14]

String operator+ ( char  c) const

Sum operator for char.

◆ operator+() [2/14]

String operator+ ( const char *  s) const

Sum operator for char*.

◆ operator+() [3/14]

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

Sum operator for std::string.

◆ operator+() [4/14]

String operator+ ( const String s) const

Sum operator for String.

◆ operator+() [5/14]

String operator+ ( double  d) const

Sum operator for double.

◆ operator+() [6/14]

String operator+ ( float  f) const

Sum operator for float.

◆ operator+() [7/14]

String operator+ ( int  i) const

Sum operator for an integer.

◆ operator+() [8/14]

String operator+ ( long double  ld) const

Sum operator for long double.

◆ operator+() [9/14]

String operator+ ( long int  i) const

Sum operator for an integer.

◆ operator+() [10/14]

String operator+ ( long long unsigned int  i) const

Sum operator for an unsigned integer.

◆ operator+() [11/14]

String operator+ ( long unsigned int  i) const

Sum operator for an unsigned integer.

◆ operator+() [12/14]

String operator+ ( short int  i) const

Sum operator for an integer.

◆ operator+() [13/14]

String operator+ ( short unsigned int  i) const

Sum operator for an unsigned integer.

◆ operator+() [14/14]

String operator+ ( unsigned int  i) const

Sum operator for an unsigned integer.

◆ operator+=() [1/14]

String& operator+= ( char  c)

Sum operator for char.

◆ operator+=() [2/14]

String& operator+= ( const char *  s)

Sum operator for char*.

◆ operator+=() [3/14]

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

Sum operator for std::string.

◆ operator+=() [4/14]

String& operator+= ( const String s)

Sum operator for String.

◆ operator+=() [5/14]

String& operator+= ( double  d)

Sum operator for double.

◆ operator+=() [6/14]

String& operator+= ( float  f)

Sum operator for float.

◆ operator+=() [7/14]

String& operator+= ( int  i)

Sum operator for an integer.

◆ operator+=() [8/14]

String& operator+= ( long double  d)

Sum operator for long double.

◆ operator+=() [9/14]

String& operator+= ( long int  i)

Sum operator for an integer.

◆ operator+=() [10/14]

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

Sum operator for an unsigned integer.

◆ operator+=() [11/14]

String& operator+= ( long unsigned int  i)

Sum operator for an unsigned integer.

◆ operator+=() [12/14]

String& operator+= ( short int  i)

Sum operator for an integer.

◆ operator+=() [13/14]

String& operator+= ( short unsigned int  i)

Sum operator for an unsigned integer.

◆ operator+=() [14/14]

String& operator+= ( unsigned int  i)

Sum operator for an unsigned integer.

◆ operator=() [1/2]

String& operator= ( const String )
default

Assignment operator.

◆ operator=() [2/2]

String& operator= ( String &&  ) &
default

Move assignment operator.

◆ prefix() [1/3]

String prefix ( char  delim) const

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

Exceptions
Exception::ElementNotFoundis thrown if delim is not found

◆ prefix() [2/3]

String prefix ( Int  length) const

returns the prefix of length length

Exceptions
Exception::IndexUnderflowis thrown if length is smaller than zero
Exception::IndexOverflowis thrown if length is bigger than the size

◆ prefix() [3/3]

String prefix ( SizeType  length) const

returns the prefix of length length

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

Referenced by OpenSwathHelper::computeTransitionGroupId(), XMassFile::importExperimentalSettings(), XMassFile::load(), and OpenMS::StringUtils::split().

◆ quote()

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()

Referenced by PeptideHit::PeakAnnotation::writePeakAnnotationsString_().

◆ random()

static String random ( UInt  length)
static

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

◆ remove()

◆ removeWhitespaces()

String& removeWhitespaces ( )

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

◆ reverse()

String& reverse ( )

inverts the direction of the string

◆ simplify()

String& simplify ( )

merges subsequent whitespaces to one blank character

Referenced by MascotGenericFile::getNextSpectrum_(), and MS2File::load().

◆ split() [1/2]

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 OpenSwathHelper::computeTransitionGroupId(), ListUtils::create(), extractCachedMetaFilename(), MascotGenericFile::getNextSpectrum_(), MsInspectFile::load(), SpecArrayFile::load(), MS2File::load(), DTA2DFile::load(), DTAFile::load(), NucleicAcidSearchEngine::parseAdduct_(), DigestionEnzymeDB< DigestionEnzymeType, InstanceType >::readEnzymesFromFile_(), and OpenMS::StringUtils::substitute().

◆ split() [2/2]

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().

◆ split_quoted()

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::ConversionErroris thrown if quotation marks are not balanced
See also
concatenate(), quote().

◆ substitute() [1/2]

String& substitute ( char  from,
char  to 
)

◆ substitute() [2/2]

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

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

◆ substr()

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
posPosition 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.
nLength 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_(), MascotGenericFile::getNextSpectrum_(), DTA2DFile::load(), OpenMS::StringUtils::prefix(), OpenMS::StringUtils::split(), OpenMS::StringUtils::split_quoted(), OpenMS::StringUtils::suffix(), and OpenMS::StringUtils::unquote().

◆ suffix() [1/3]

String suffix ( char  delim) const

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

Exceptions
Exception::ElementNotFoundis thrown if delim is not found

◆ suffix() [2/3]

String suffix ( Int  length) const

returns the suffix of length length

Exceptions
Exception::IndexUnderflowis thrown if length is smaller than zero
Exception::IndexOverflowis thrown if length is bigger than the size

◆ suffix() [3/3]

String suffix ( SizeType  length) const

returns the suffix of length length

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

Referenced by XMassFile::load(), and OpenMS::StringUtils::split().

◆ toDouble()

double toDouble ( ) const

◆ toFloat()

float toFloat ( ) const

Conversion to float.

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

Referenced by XMLHandler::asFloat_().

◆ toInt()

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::ConversionErroris thrown if the string could not be converted to Int

Referenced by XMLHandler::asInt_(), XMLHandler::asUInt_(), and MascotGenericFile::getNextSpectrum_().

◆ toInt32()

Int32 toInt32 ( ) const

Conversion to Int32.

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

Exceptions
Exception::ConversionErroris thrown if the string could not be converted to Int32

Referenced by OpenMS::detail::convert(), and XMLHandler::fromXSDString().

◆ toInt64()

Int64 toInt64 ( ) const

Conversion to Int64.

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

Exceptions
Exception::ConversionErroris thrown if the string could not be converted to Int64

Referenced by XMLHandler::fromXSDString().

◆ toLower()

◆ toQString()

◆ toUpper()

String& toUpper ( )

Converts the string to uppercase.

Referenced by DTA2DFile::load().

◆ trim()

String& trim ( )

removes whitespaces (space, tab, line feed, carriage return) at the beginning and the end of the string

Referenced by XMLHandler::asDateTime_(), ListUtils::create(), MascotGenericFile::getNextSpectrum_(), MS2File::load(), DTA2DFile::load(), DTAFile::load(), and OpenMS::StringUtils::split().

◆ unquote()

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::ConversionErroris thrown if the string does not have the format produced by quote
See also
quote()

Member Data Documentation

◆ EMPTY

const String EMPTY
static

Empty string for comparisons.