CLASS  1.1
 Tout Classes Fichiers Fonctions Variables Définitions de type
Référence de la classe StringLine

#include "StringLine.hxx"

Fonctions membres publiques

string ReplaceAll (string InLine, string ToReplace, string By)
 
template<class out_T , class in_T >
out_T convert (const in_T &t)
 

Fonctions membres publiques statiques

static string NextWord (string Line, int &start, char sep=' ', char alt_sep='\0')
 
static string PreviousWord (string Line, int &start, char sep=' ', char alt_sep='\0')
 
static void ToLower (string &Line)
 
static void ToUpper (string &Line)
 
static int Find (const char *search, string Line)
 
static int rFind (const char *search, string Line)
 
template<class out_T , class in_T >
static out_T convert (const in_T &t)
 
static int GetStartWord (string Line, int CurrentPosition, char sep=' ', char alt_sep='\0')
 
static int GetEndWord (string Line, int CurrentPosition, char sep=' ', char alt_sep='\0')
 

Description détaillée

The aim of this class is to provide tools to extract fields ("word") from a string and convert a string in Upper/Lower case. All methods are static so that it is not necessary to create object to use them

example:

string line="The temperature is : 300.6 K";
int start;
1st method: creation of StringLine
start=0;
string the=SL.NextWord(line,start);
string temperature_is=SL.NextWord(line,start,':');
string colon=SL.NextWord(line,start);
double T=atof(SL.NextWord(line,start).c_str());
cout<<the<<endl<<temperature_is<<endl<<T<<endl;
2nd method: "using" the static methods
start=0;
the=StringLine::NextWord(line,start);
temperature_is=StringLine::NextWord(line,start,':');
colon=StringLine::NextWord(line,start);
T=atof(StringLine::NextWord(line,start).c_str());
cout<<the<<endl<<temperature_is<<endl<<T<<endl;
Auteur
PTO
Version
2.01

Définition à la ligne 49 du fichier StringLine.hxx.

Documentation des fonctions membres

template<class out_T , class in_T >
static out_T StringLine::convert ( const in_T &  t)
static

Example:

string s="32.12";
double t=StringLine::convert<double>(s);
string temperature=StringLine::convert<string>(300.);
Paramètres
t: the input value
template<class out_T , class in_T >
out_T StringLine::convert ( const in_T &  t)
inline

Définition à la ligne 252 du fichier StringLine.hxx.

253 {
254  stringstream stream;
255  stream << t; // insert value to stream
256  out_T result; // store conversion's result here
257  stream >> result; // write value to result
258  return result;
259 }
int StringLine::Find ( const char *  search,
string  Line 
)
inlinestatic

returns the position, starting from the begenning of the first occurence of search in Line if it is found, else returns -1

Paramètres
search: a string to find
Line: where to search

Définition à la ligne 235 du fichier StringLine.hxx.

236 {
237  size_t Pos=Line.find(search);
238  if(Pos != string::npos ) return Pos;
239  return -1;
240 }
int StringLine::GetEndWord ( string  Line,
int  CurrentPosition,
char  sep = ' ',
char  alt_sep = '\0' 
)
inlinestatic
Paramètres
Line: a line containing words
CurrentPosition: from where to start to find the end of a word
sep: the separator between 2 words (default=space)
alt_sep: the alternative separator between 2 words (default='')

Définition à la ligne 217 du fichier StringLine.hxx.

218 {
219  int pos=Line.find(sep,CurrentPosition);
220  int alt_pos=-1;
221  if(alt_sep!='\0')
222  alt_pos=Line.find(alt_sep,CurrentPosition);
223  int real_pos=pos;
224  if(alt_pos>=0)
225  {
226  real_pos=min(pos,alt_pos);
227  if(pos==int(string::npos))real_pos=alt_pos;
228  }
229  if(real_pos==int(string::npos))
230  return Line.size();
231  return real_pos;
232 }
int StringLine::GetStartWord ( string  Line,
int  CurrentPosition,
char  sep = ' ',
char  alt_sep = '\0' 
)
inlinestatic
Paramètres
Line: a line containing words
CurrentPosition: from where to start to find the begining of a word
sep: the separator between 2 words (default=space)
alt_sep: the alternative separator between 2 words (default='')

Définition à la ligne 195 du fichier StringLine.hxx.

196 {
197  int pos=Line.find(sep,CurrentPosition);
198  int alt_pos=-1;
199  if(alt_sep!='\0')
200  alt_pos=Line.find(alt_sep,CurrentPosition);
201  int real_pos=pos;
202  char real_sep=sep;
203  if(alt_pos>=0)
204  {
205  real_pos=min(pos,alt_pos);
206  if(pos==int(string::npos))real_pos=alt_pos;
207  if(real_pos!=pos)
208  real_sep=alt_sep;
209  }
210  if(real_pos==int(string::npos)) return CurrentPosition;
211  while(CurrentPosition<int(Line.size()) && Line[CurrentPosition]==real_sep)
212  CurrentPosition++;
213  return CurrentPosition;
214 }
string StringLine::NextWord ( string  Line,
int &  start,
char  sep = ' ',
char  alt_sep = '\0' 
)
inlinestatic

Find Next word in a line starting from position "start" in the line. If an alternative separator is given, the word length is defined by the first position of sep or alt_sep found. The first value of start is in general 0 (i.e. the beginning of the Line)

Paramètres
Line: a line containing words
start: from where to start to find the begining of a word
sep: the separator between 2 words (default=space)
alt_sep: the alternative separator between 2 words (default='')

Définition à la ligne 131 du fichier StringLine.hxx.

132 {
133  string Word="";
134  if(start>=int(Line.size()))
135  {
136  return Word;
137  }
138  start=GetStartWord(Line,start,sep,alt_sep);
139  int wordlength=GetEndWord(Line,start,sep,alt_sep)-start;
140 
141  Word=Line.substr(start,wordlength);
142 
143  start+=wordlength;
144  return Word;
145 }
string StringLine::PreviousWord ( string  Line,
int &  start,
char  sep = ' ',
char  alt_sep = '\0' 
)
inlinestatic

Find Previous word in a line starting from position "start" in the line. If an alternative separator is given, the word length is defined by the first position of sep or alt_sep found. The first value of start is in general the end of the Line.

Paramètres
Line: a line containing words
start: from where to start to find the begining of a word
sep: the separator between 2 words (default=space)
alt_sep: the alternative separator between 2 words (default='')

Définition à la ligne 147 du fichier StringLine.hxx.

148 {
149  string Word="";
150  if(start<=0)
151  {
152  return Word;
153  }
154  int pos=Line.rfind(sep,start);
155  int alt_pos=-1;
156  int real_pos=pos;
157  char real_sep=sep;
158  if(alt_sep!='\0')
159  {
160  alt_pos=Line.rfind(alt_sep,start);
161  real_pos=max(pos,alt_pos);
162  if(real_pos!=pos)
163  real_sep=alt_sep;
164  }
165  int wordlength=start-Line.rfind(real_sep,real_pos);
166  if(real_pos<=0)
167  {
168  Word=Line.substr(0,start+1);
169  start=0;
170  return Word;
171  }
172  Word=Line.substr(real_pos+1,wordlength);
173 
174  start-=wordlength+1;
175  return Word;
176 }
string StringLine::ReplaceAll ( string  InLine,
string  ToReplace,
string  By 
)
inline
Paramètres
InLine: the string which contains the sub-string to replace
ToReplace: the sub-string to replace
By: the sub-string ToReplace is replaced by the sub-string By in Inline

Définition à la ligne 262 du fichier StringLine.hxx.

263 {
264  int start=0;
265  int pos=InLine.find(ToReplace,start);
266  while(pos!=int(string::npos))
267  {
268  InLine.replace(pos,ToReplace.size(),By);
269  start=0;
270  pos=InLine.find(ToReplace,start);
271  }
272  return InLine;
273 
274 }
int StringLine::rFind ( const char *  search,
string  Line 
)
inlinestatic

returns the position, starting from the end of the first occurence of search in Line if it is found, else returns -1

Paramètres
search: a string to find
Line: where to search

Définition à la ligne 243 du fichier StringLine.hxx.

244 {
245  size_t Pos=Line.rfind(search);
246  if(Pos != string::npos) return Pos;
247  return -1;
248 }
void StringLine::ToLower ( string &  Line)
inlinestatic

Définition à la ligne 179 du fichier StringLine.hxx.

180 {
181  transform (Line.begin(), Line.end(), // source
182  Line.begin(), // destination
183  (int(*)(int))tolower); // operation
184 }
void StringLine::ToUpper ( string &  Line)
inlinestatic

Définition à la ligne 187 du fichier StringLine.hxx.

188 {
189  transform (Line.begin(), Line.end(), // source
190  Line.begin(), // destination
191  (int(*)(int))toupper); // operation
192 }

La documentation de cette classe a été générée à partir du fichier suivant :