MURE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
Static Public Member Functions | List of all members
StringLine Class Reference

Class extracting fields from a string / line. More...

#include <StringLine.hxx>

Inheritance diagram for StringLine:
XSDIRLine

Static Public Member Functions

static string NextWord (string Line, int &start, char sep=' ', char alt_sep='\0')
 Find the next word in a line. More...
 
static string PreviousWord (string Line, int &start, char sep=' ', char alt_sep='\0')
 Find the previous word in a line. More...
 
static void ToLower (string &Line)
 convert a string to Lower case More...
 
static void ToUpper (string &Line)
 convert a string to Upper case More...
 
static int Find (const char *search, string Line)
 Find search in Line from the begining. More...
 
static int rFind (const char *search, string Line)
 Find search in Line from the end. More...
 
template<class out_T , class in_T >
static out_T convert (const in_T &t)
 convert a input type (in_T) to another (out_T). More...
 
template<class T >
static bool ToNumber (T &t, const std::string &s, std::ios_base &(*f)(std::ios_base &))
 try to convert a string to a number. More...
 
static int GetStartWord (string Line, int CurrentPosition, char sep=' ', char alt_sep='\0')
 Find the start of a word in a line. More...
 
static int GetEndWord (string Line, int CurrentPosition, char sep=' ', char alt_sep='\0')
 Find the end of a word in a line. More...
 
static string ReplaceAll (string InLine, string ToReplace, string By)
 Replace a sub-string by an other in a string. More...
 
static bool IsDouble (const std::string &s)
 

Detailed Description

Class extracting fields from a string / line.

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;
Author
PTO
Version
0.1

Member Function Documentation

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

convert a input type (in_T) to another (out_T).

Example:

string s="32.12";
double t=StringLine::convert<double>(s);
string temperature=StringLine::convert<string>(300.);
Parameters
t: the input value
int StringLine::Find ( const char *  search,
string  Line 
)
inlinestatic

Find search in Line from the begining.

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

Parameters
search: a string to find
Line: where to search
int StringLine::GetEndWord ( string  Line,
int  CurrentPosition,
char  sep = ' ',
char  alt_sep = '\0' 
)
inlinestatic

Find the end of a word in a line.

Parameters
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='')
int StringLine::GetStartWord ( string  Line,
int  CurrentPosition,
char  sep = ' ',
char  alt_sep = '\0' 
)
inlinestatic

Find the start of a word in a line.

Parameters
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='')
bool StringLine::IsDouble ( const std::string &  s)
inlinestatic
string StringLine::NextWord ( string  Line,
int &  start,
char  sep = ' ',
char  alt_sep = '\0' 
)
inlinestatic

Find the next word in a line.

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)

Parameters
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='')
string StringLine::PreviousWord ( string  Line,
int &  start,
char  sep = ' ',
char  alt_sep = '\0' 
)
inlinestatic

Find the previous word in a line.

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.

Parameters
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='')
string StringLine::ReplaceAll ( string  InLine,
string  ToReplace,
string  By 
)
inlinestatic

Replace a sub-string by an other in a string.

Parameters
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
int StringLine::rFind ( const char *  search,
string  Line 
)
inlinestatic

Find search in Line from the end.

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

Parameters
search: a string to find
Line: where to search
void StringLine::ToLower ( string &  Line)
inlinestatic

convert a string to Lower case

template<class T >
static bool StringLine::ToNumber ( T &  t,
const std::string &  s,
std::ios_base &(*)(std::ios_base &)  f 
)
inlinestatic

try to convert a string to a number.

Example:

string s="32.12";
double d;
if(StringLine::ToNumber(d,s,std::dec))
cout<<"double="<<d<<endl;
string hexanum="ff";
int i;
if(StringLine::ToNumber(i,hexanum,std::hex))
cout<<"int="<<i<<endl;
Parameters
s: the input string
t: the output value
f: string format (ie hex, dec, oct)
void StringLine::ToUpper ( string &  Line)
inlinestatic

convert a string to Upper case


The documentation for this class was generated from the following file:

MURE Project, documentation generated by Doxygen 1.8.5 - Mon Nov 17 2014