-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathStringTools.h
60 lines (52 loc) · 1.51 KB
/
StringTools.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef __StringTools_h__
#define __StringTools_h__
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
namespace Utilities
{
/** \brief Tools to handle std::string objects
*/
class StringTools
{
public:
static void tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters = " ")
{
std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
std::string::size_type pos = str.find_first_of(delimiters, lastPos);
while (std::string::npos != pos || std::string::npos != lastPos)
{
tokens.push_back(str.substr(lastPos, pos - lastPos));
lastPos = str.find_first_not_of(delimiters, pos);
pos = str.find_first_of(delimiters, lastPos);
}
}
/** converts a value to a string with a given precistion */
template <typename T>
static std::string to_string_with_precision(const T val, const int n = 6)
{
std::ostringstream ostr;
ostr.precision(n);
ostr << std::fixed << val;
return ostr.str();
}
/** converts a double or a float to a string */
template<typename T>
static std::string real2String(const T r)
{
std::string str = to_string_with_precision(r,20);
str.erase(str.find_last_not_of('0') + 1, std::string::npos);
str.erase(str.find_last_not_of('.') + 1, std::string::npos);
return str;
}
static std::string to_upper(const std::string& str)
{
std::string res = str;
std::transform(res.begin(), res.end(), res.begin(), ::toupper);
return res;
}
};
}
#endif