bpp-core  2.2.0
TextTools.h
Go to the documentation of this file.
1 //
2 // File: TextTools.h
3 // Created by: Julien Dutheil
4 // Created on: Fri Aug 8 12:57:50 2003
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
9 
10  This software is a computer program whose purpose is to provide basal and
11  utilitary classes. This file belongs to the Bio++ Project.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided only
21  with a limited warranty and the software's author, the holder of the
22  economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards their
32  requirements in conditions enabling the security of their systems and/or
33  data to be ensured and, more generally, to use and operate it in the
34  same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38  */
39 
40 #ifndef _TEXTTOOLS_H_
41 #define _TEXTTOOLS_H_
42 
43 #include "../Exceptions.h"
44 
45 
46 // From the STL:
47 #include <string>
48 #include <vector>
49 #include <sstream>
50 #include <iomanip>
51 
52 namespace bpp
53 {
54 
58  class TextTools
59  {
60  public:
61 
71  static bool isEmpty(const std::string& s);
72 
79  static std::string toUpper(const std::string& s);
80 
87  static std::string toLower(const std::string& s);
88 
95  static bool isWhiteSpaceCharacter(char c);
96 
103  static std::string removeWhiteSpaces (const std::string& s);
104 
111  static std::string removeFirstWhiteSpaces (const std::string& s);
112 
119  static std::string removeLastWhiteSpaces (const std::string& s);
120 
128  static std::string removeSurroundingWhiteSpaces(const std::string& s);
129 
136  static bool isNewLineCharacter(char c);
137 
144  static std::string removeNewLines (const std::string& s);
145 
152  static std::string removeLastNewLines(const std::string& s);
153 
160  static bool isDecimalNumber(char c);
161 
171  static bool isDecimalNumber(const std::string& s, char dec = '.', char scientificNotation = 'e');
172 
181  static bool isDecimalInteger(const std::string& s, char scientificNotation = 'e');
182 
189  template<class T> static std::string toString(T t)
190  {
191  std::ostringstream oss;
192  oss << t;
193  return oss.str();
194  }
195 
203  template<class T>
204  static std::string toString(T t, int precision)
205  {
206  std::ostringstream oss;
207  oss << std::setprecision(precision) << t;
208  return oss.str();
209  }
210 
217  template<class T> static T fromString(const std::string& s)
218  {
219  std::istringstream iss(s);
220  T obj;
221  iss >> obj;
222  return obj;
223  }
224 
231  static std::string toString(int i);
232 
239  static std::string toString(char c);
240 
248  static std::string toString(double d, int precision = 6);
249 
258  static int toInt(const std::string& s, char scientificNotation = 'e') throw (Exception);
259 
269  static double toDouble(const std::string& s, char dec = '.', char scientificNotation = 'e') throw (Exception);
270 
277  template<class T>
278  static T to(const std::string& s)
279  {
280  std::istringstream iss(s);
281  T t;
282  iss >> t;
283  return t;
284  }
285 
295  static std::string resizeRight(const std::string& s, size_t newSize, char fill = ' ');
296 
306  static std::string resizeLeft(const std::string& s, size_t newSize, char fill = ' ');
307 
317  static std::vector<std::string> split(const std::string& s, size_t n);
318 
332  static std::string removeSubstrings(const std::string& s, char blockBeginning, char blockEnding)
333  throw (Exception);
334 
352  static std::string removeSubstrings(const std::string& s, char blockBeginning, char blockEnding, std::vector<std::string>& exceptionsBeginning, std::vector<std::string>& exceptionsEnding)
353  throw (Exception);
354 
362  static std::string removeChar(const std::string& s, char c);
363 
371  static unsigned int count(const std::string& s, const std::string& pattern);
372 
380  static bool startsWith(const std::string& s, const std::string& pattern);
381 
389  static bool endsWith(const std::string& s, const std::string& pattern);
390 
398  static bool hasSubstring(const std::string& s, const std::string& pattern);
399 
407  static void replaceAll(std::string& target, const std::string& query, const std::string& replacement);
408 
409  };
410 
411 } //end of namespace bpp.
412 
413 #endif //_TEXTTOOLS_H_
414 
static T fromString(const std::string &s)
General template method to convert from string.
Definition: TextTools.h:217
static bool isNewLineCharacter(char c)
Tell if a character is a new line character or not.
Definition: TextTools.cpp:160
static std::string resizeLeft(const std::string &s, size_t newSize, char fill=' ')
Send a string of size &#39;newSize&#39;, which is a copy of &#39;s&#39; truncated or filled with character &#39;fill&#39; at ...
Definition: TextTools.cpp:334
This class allows to perform a correspondence analysis.
static std::string resizeRight(const std::string &s, size_t newSize, char fill=' ')
Send a string of size &#39;newSize&#39;, which is a copy of &#39;s&#39; truncated or filled with character &#39;fill&#39; at ...
Definition: TextTools.cpp:324
STL namespace.
static bool isDecimalNumber(char c)
Tell is a given character describes a decimal number.
Definition: TextTools.cpp:205
static std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:189
static int toInt(const std::string &s, char scientificNotation='e')
Convert from string to int.
Definition: TextTools.cpp:302
static unsigned int count(const std::string &s, const std::string &pattern)
Count the occurences of a given pattern in a string.
Definition: TextTools.cpp:473
static bool startsWith(const std::string &s, const std::string &pattern)
Tell is a string begins with a certain motif.
Definition: TextTools.cpp:487
static bool hasSubstring(const std::string &s, const std::string &pattern)
Tell is a string contains a certain motif.
Definition: TextTools.cpp:505
static bool isEmpty(const std::string &s)
Tell if a string is empty.
Definition: TextTools.cpp:52
static std::string removeSurroundingWhiteSpaces(const std::string &s)
Remove all white spaces characters at the beginning and the end of a string.
Definition: TextTools.cpp:153
static bool endsWith(const std::string &s, const std::string &pattern)
Tell is a string ends with a certain motif.
Definition: TextTools.cpp:496
static std::string removeNewLines(const std::string &s)
Remove all new line characters in a string.
Definition: TextTools.cpp:168
static T to(const std::string &s)
Template to string conversion.
Definition: TextTools.h:278
static std::string removeWhiteSpaces(const std::string &s)
Remove all white spaces characters in a string.
Definition: TextTools.cpp:100
static bool isDecimalInteger(const std::string &s, char scientificNotation='e')
Tell is a given character string describes a decimal integer.
Definition: TextTools.cpp:247
static std::string removeFirstWhiteSpaces(const std::string &s)
Remove all white spaces characters at the beginning of a string.
Definition: TextTools.cpp:121
static std::string toString(T t, int precision)
Template string conversion.
Definition: TextTools.h:204
Exception base class.
Definition: Exceptions.h:57
static std::string removeLastWhiteSpaces(const std::string &s)
Remove all white spaces characters at the end of a string.
Definition: TextTools.cpp:137
static std::string removeLastNewLines(const std::string &s)
Remove all new line characters at the end of a string.
Definition: TextTools.cpp:189
static std::vector< std::string > split(const std::string &s, size_t n)
Split a string into parts of size &#39;n&#39;.
Definition: TextTools.cpp:344
static std::string removeSubstrings(const std::string &s, char blockBeginning, char blockEnding)
Remove substrings from a string.
Definition: TextTools.cpp:359
static std::string removeChar(const std::string &s, char c)
Remove all occurences of a character in a string.
Definition: TextTools.cpp:452
static std::string toUpper(const std::string &s)
Make the string uppercase.
Definition: TextTools.cpp:65
static std::string toLower(const std::string &s)
Make the string lowercase.
Definition: TextTools.cpp:77
static void replaceAll(std::string &target, const std::string &query, const std::string &replacement)
Replacement of all non-overlapping occurrences of a certain motif in a string.
Definition: TextTools.cpp:519
Some utilitary functions that work on strings.
Definition: TextTools.h:58
static double toDouble(const std::string &s, char dec='.', char scientificNotation='e')
Convert from string to double.
Definition: TextTools.cpp:313
static bool isWhiteSpaceCharacter(char c)
Tell if a character is a white space or not.
Definition: TextTools.cpp:89