StringHelper.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
23 #include "icl_core/StringHelper.h"
24 
25 namespace icl_core {
26 
27 std::vector<String> split(const String& str, const String& delimiter)
28 {
29  String s = str;
30  String::size_type pos = 0;
31  std::vector<String> substrings;
32  if (s.empty())
33  {
34  substrings.push_back("");
35  return substrings;
36  }
37  while ((pos = s.find(delimiter)) != String::npos)
38  {
39  substrings.push_back(s.substr(0, pos));
40  s.erase(0, pos+delimiter.size());
41  }
42  if (!s.empty())
43  {
44  substrings.push_back(s);
45  }
46  return substrings;
47 }
48 
49 String join(const std::vector<String>& substrings, const String& delimiter)
50 {
51  String result;
52  for (std::vector<String>::const_iterator it = substrings.begin(); it != substrings.end(); ++it)
53  {
54  if (it != substrings.begin())
55  {
56  result += delimiter;
57  }
58  result += *it;
59  }
60  return result;
61 }
62 
63 String trim(String const & str)
64 {
65  std::string result = "";
66 
67  std::string::size_type length = str.length();
68 
69  std::string::size_type trim_front = 0;
70  while ((trim_front < length) && isspace(static_cast<unsigned char>(str[trim_front])))
71  {
72  ++trim_front;
73  }
74 
75  std::string::size_type trim_end = length - 1;
76  while ((trim_end > trim_front) && isspace(static_cast<unsigned char>(str[trim_end])))
77  {
78  --trim_end;
79  }
80 
81  if (trim_front == length)
82  {
83  result = "";
84  }
85  else
86  {
87  result = str.substr(trim_front, trim_end - trim_front + 1);
88  }
89 
90  return result;
91 }
92 
94 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
95 
96 String Trim(String const & str)
97 {
98  return trim(str);
99 }
100 
101 #endif
102 
104 }
Contains helper functions for dealing with String.
std::vector< String > split(const String &str, const String &delimiter)
String trim(String const &str)
String join(const std::vector< String > &substrings, const String &delimiter)
std::string String
Definition: BaseTypes.h:43


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58