📄 stringutils.hpp
字号:
* Convert an int to a string. * @param i the integer to convert * @return a string with the hex equivalent of i */ inline std::string int2x(const unsigned int& i) throw(StringException); /** * Replace all instances of \a oldString with \a newString in \a s. * @param s the string whose contents will be modified. * @param oldString the string to search for in \a s. * @param newString the string to replace \a oldString in \a s. * @return a reference to the modified string. */ inline std::string& replaceAll(std::string& s, const std::string& oldString, const std::string& newString ) throw(StringException); /** * isDigitString is exactly like the C function isDigit * except it checks all the characters of string \a s to see if * they are all digits. * @param s the string to check the digits in. * @return true if \a s is all digits, false otherwise. */ inline bool isDigitString(const std::string& s); /** * isDecimalString is like isDigitString() except it allows a * single period ('.') character in the string. * @param s the string to check. * @return true if \a s is a valid fixed-point number. */ inline bool isDecimalString(const std::string& s); /** * isAlphaString is exactly like the C function isAlpha * except it checks all the characters of string \a s to see if * they are all alphabet characters. * @param s the string to check the characters in. * @return true if \a s is all digits, false otherwise. */ inline bool isAlphaString(const std::string& s); /** * Perform pattern matching on strings. * Looks for a pattern in a string. Wildcards are allowed. * Uses POSIX regular expressions. * @param s string to search. * @param aPattern pattern to search for. This is a POSIX * regular expression. * @param zeroOrMore character representing wildcards * matching strings of zero or more characters (default '*'). * @param oneOrMore character representing plus sign * matching strings of one or more characters (default '+'). * @param anyChar character representing wildcards matching a * single arbitrary character (default '.'). * @return string representing the first match of \a aPattern in * \a s. Returns a null string if no match is found. */ inline std::string matches(const std::string& s, const std::string& aPattern, const char zeroOrMore = '*', const char oneOrMore = '+', const char anyChar = '.' ) throw(StringException); /** * Perform pattern matching on strings. * Looks for a pattern in a string. Wildcards are allowed. * Uses POSIX regular expressions. * @param s string to search. * @param aPattern pattern to search for. This is a POSIX * regular expression. * @param zeroOrMore character representing wildcards * matching strings of zero or more characters (default '*'). * @param oneOrMore character representing plus sign * matching strings of one or more characters (default '+'). * @param anyChar character representing wildcards matching a * single arbitrary character (default '.'). * @return t if a match is found, f if not. */ inline bool isLike(const std::string& s, const std::string& aPattern, const char zeroOrMore = '*', const char oneOrMore = '+', const char anyChar = '.' ) throw(StringException) { return matches(s, aPattern, zeroOrMore, oneOrMore, anyChar) != std::string(); } /** * Perform pattern matching on strings. * Looks for a pattern in a string. Wildcards are allowed. * Uses POSIX regular expressions. * @param s string to search. * @param pPattern pattern to search for. This is a POSIX * regular expression. * @param zeroOrMore character representing wildcards * matching strings of zero or more characters (default '*'). * @param oneOrMore character representing plus sign * matching strings of one or more characters (default '+'). * @param anyChar character representing wildcards matching a * single arbitrary character (default '.'). * @return t if a match is found, f if not. */ inline bool isLike(const std::string& s, const char* pPattern, const char zeroOrMore = '*', const char oneOrMore = '+', const char anyChar = '.' ) throw(StringException) { return matches(s, std::string(pPattern), zeroOrMore, oneOrMore, anyChar) != std::string(); } /** * Work-horse method for printf. Substitutes patterns * matching \a pat with \a rep. Use only one pattern/token * at a time! This used to be DayTime::iprint(). * @param fmt format to use for this time. * @param pat regular expression pattern to match. * @param rep sprintf token replacement. First character is * token character used in fmt, remainder is sprintf token to * use. For example, with fmt="%15S", pat="%[ 0-]?[[:digit:]]*S", * and rep="Sd", the fmt will be translated to "%15d" before * using it in a sprintf call like printf("%15d"), \a to. * @param to the value to stuff into the string. * @return \a fmt with \a pat replaced by \a to. If there is no * match, \a fmt is returned unchanged. */ template <class T> std::string formattedPrint(const std::string& fmt, const std::string& pat, const std::string& rep, T to) throw(StringException); /** * Get a substring of a string. * Try to avoid using this, use the stl string's substr * method instead (and ::leftJustify if needed). */ inline std::string subString(const std::string& s, const std::string::size_type startPos = 0, const std::string::size_type length = std::string::npos, const char pad = ' ' ) throw(StringException); /** * Change all upper-case letters in a string to lower-case. * \a s is modified as a result. * @param s string to change to lower case. * @return a copy of the original string, all in lower-case. */ inline std::string& lowerCase(std::string& s); /** * Change all upper-case letters in a string to lower-case. * Does not modify the original string. * @param s a string containing upper-case characters. * @return a copy of the original string, all in lower-case. */ inline std::string lowerCase(const std::string& s) { std::string t(s); return lowerCase(t); } /** * Change all lower-case letters in a string to upper-case. * \a s is modified as a result. * @param s string to change to upper case. * @return a copy of the original string, all in upper-case. */ inline std::string& upperCase(std::string& s); /** * Change all lower-case letters in a string to upper-case. * Does not modify the original string. * @param s a string containing lower-case characters. * @return a copy of the original string, all in upper-case. */ inline std::string upperCase(const std::string& s) { std::string t(s); return upperCase(t); } /** * Make a string from a void pointer. * This function should not be used. Instead, use the string * constructor as follows: * \code string((char*)p, size); \endcode * @param p pointer to memory. * @param size length of the data to turn into a string. * @return string object containing the contents of \a p. */ inline std::string memToString(const void* p, const std::string::size_type size); /** * Returns the first word in string \a s without modifying the string. * @param s the string to count the words from. * @param delimiter the character that marks the start and * end of a word. * @return the first word from \a s; */ inline std::string firstWord(const std::string& s, const char delimiter = ' ') throw(StringException); /** * Counts the number of words in \a s and returns it. * @param s the string to count the words from. * @param delimiter the character that marks the start and * end of a word. * @return the number of words in \a s. */ inline int numWords(const std::string& s, const char delimiter = ' ') throw(StringException); /** * Returns \a numWords words starting with \a firstWord from * \a s (if any). * @param s a string with the word you want removed. * @param firstWord the number of the first word you want from \a s. * The first word is word 0. * @param numWords number of words to get from \a s. * @param delimiter the character that marks the start and * end of a word. * @return the first word from \a s or an empty string if there is * no \a wordNum'th word. */ inline std::string words(const std::string& s, const std::string::size_type firstWord = 0, const std::string::size_type numWords = std::string::npos, const char delimiter = ' ') throw(StringException); /** * Returns word number \a wordNum from \a s (if any). * @param s a string with the word you want removed. * @param wordNum the number of the word you want from \a s. * The first word is word 0. * @param delimiter the character that marks the start and * end of a word. * @return the first word from \a s or an empty string if there is * no \a wordNum'th word. */ inline std::string word(const std::string& s, const std::string::size_type wordNum = 0, const char delimiter = ' ') throw(StringException) { return words(s, wordNum, 1, delimiter); } /** * Removes the first word off string \a s and returns it. * \a s is modified as a result. * @param s a string with the word you want removed. * @param delimiter the character that marks the start and * end of a word. * @return the first word from \a s */ inline std::string stripFirstWord(std::string& s, const char delimiter = ' ') throw(StringException); /** * Removes indicated words from the string \a s. * \a s is modified as a result. * @param s a string with the words you want removed. * @param first the first word to be removed (the first word is 0). * @param wordsToReplace the number of words you want removed. * @param delimiter the character that marks the start and * end of a word. * @return a reference to string \a s with the words removed. */ inline std::string& removeWords(std::string& s, const std::string::size_type first = 0, const std::string::size_type wordsToReplace = std::string::npos, const char delimiter = ' ') throw(StringException); /** * Convert a double to a scientific notation number. * @param d the double to convert * @param length length (in characters) of output, including exponent * @param expLen length (in characters) of the exponent, with sign * @param showSign if true, reserves 1 character for +/- sign * @param checkSwitch if true, keeps the exponential sanity check for * exponentials above three characters in length. If false, it removes * that check. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -