MURE
Loading...
Searching...
No Matches
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

Extracting word from string
static string NextWord (string Line, int &start, char sep=' ', char alt_sep='\0')
 Find the next word in a line.
 
static string PreviousWord (string Line, int &start, char sep=' ', char alt_sep='\0')
 Find the previous word in a line.
 
static int GetStartWord (string Line, int CurrentPosition, char sep=' ')
 Find the start of a word in a line.
 
static int GetEndWord (string Line, int CurrentPosition, char sep=' ')
 Find the end of a word in a line.
 
static string ReplaceAll (string InLine, string ToReplace, string By)
 Replace a sub-string by an other in a string.
 
static void ltrim (std::string &s)
 Remove starting spaces (trim) a string.
 
static void rtrim (std::string &s)
 Remove ending spaces (trim) of a string.
 
static void trim (std::string &s)
 Remove spaces (trim) of the begining and the end of a string.
 
Searching methods
static int Find (const char *search, string Line)
 Find search in Line from the begining.
 
static int rFind (const char *search, string Line)
 Find search in Line from the end.
 
Coversion method
static void ToLower (string &Line)
 convert a string to Lower case
 
static void ToUpper (string &Line)
 convert a string to Upper case
 
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).
 
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.
 
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;
Class extracting fields from a string / line.
Definition StringLine.hxx:70
static string NextWord(string Line, int &start, char sep=' ', char alt_sep='\0')
Find the next word in a line.
Definition StringLine.hxx:207
Author
PTO
Version
0.1

Member Function Documentation

◆ convert()

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

◆ Find()

int StringLine::Find ( const char *  search,
string  Line 
)
inlinestatic

Find search in Line from the begining.

returns the position, starting from the beginning 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

◆ GetEndWord()

int StringLine::GetEndWord ( string  Line,
int  CurrentPosition,
char  sep = ' ' 
)
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)

◆ GetStartWord()

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

◆ IsDouble()

bool StringLine::IsDouble ( const std::string &  s)
inlinestatic

◆ ltrim()

void StringLine::ltrim ( std::string &  s)
inlinestatic

Remove starting spaces (trim) a string.

Parameters
s: the string is replace by the same string without starting space

◆ NextWord()

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='')

◆ PreviousWord()

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='')

◆ ReplaceAll()

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

◆ rFind()

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

◆ rtrim()

void StringLine::rtrim ( std::string &  s)
inlinestatic

Remove ending spaces (trim) of a string.

Parameters
s: the string is replace by the same string without ending spaces

◆ ToLower()

void StringLine::ToLower ( string &  Line)
inlinestatic

convert a string to Lower case

◆ ToNumber()

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;
static bool ToNumber(T &t, const std::string &s, std::ios_base &(*f)(std::ios_base &))
try to convert a string to a number.
Definition StringLine.hxx:195
Parameters
s: the input string
t: the output value
f: string format (ie hex, dec, oct)

◆ ToUpper()

void StringLine::ToUpper ( string &  Line)
inlinestatic

convert a string to Upper case

◆ trim()

void StringLine::trim ( std::string &  s)
inlinestatic

Remove spaces (trim) of the begining and the end of a string.

Parameters
s: the string is replace by the same string without starting/ending spaces

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

MURE Project, documentation generated by Doxygen 1.9.7 - Fri Jan 19 2024