summaryrefslogtreecommitdiff
path: root/src/uStringTool.h
blob: d86318474d4348d49a674bc65e0845cd7dd608bd (plain)
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
/*
 * uStringTool.h
 *
 *  Created on: 2013. 10. 30.
 *      Author: kos
 */

#ifndef USTRINGTOOL_H_
#define USTRINGTOOL_H_

#include <string>
#include <vector>
#include <sstream>

using namespace std;

#define trim_default_delimiter " \t\n\v\r"

namespace uStringTool
{
	inline std::string Trim(std::string& s, const std::string& drop = trim_default_delimiter)
	{
		std::string r = s.erase(s.find_last_not_of(drop) + 1);
		return r.erase(0, r.find_first_not_of(drop));
	}

	inline std::string RTrim(std::string s, const std::string& drop = trim_default_delimiter)
	{
		std::string r = s.erase(s.find_last_not_of(drop) + 1);
		return r;
	}

	inline std::string LTrim(std::string s, const std::string& drop = trim_default_delimiter)
	{
		std::string r = s.erase(0, s.find_first_not_of(drop));
		return r;
	}

	inline std::string ReplaceAll(const std::string &in, const std::string &entity, const std::string &symbol)
	{
		std::string out = in;
		std::string::size_type loc = 0;
		while (( loc = out.find(entity, loc)) != std::string::npos )
			out.replace(loc, entity.length(), symbol);
		return out;
	}

	inline int Split(std::string data, const char delimiter, std::vector<std::string>& tokens)
	{
			std::stringstream data_stream(data);
			for(std::string token; std::getline(data_stream, token, delimiter); tokens.push_back(Trim(token)));
			return tokens.size();
	}
};

#endif /* USTRINGTOOL_H_ */