<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">\documentclass[onecolumn,letterpaper,11pt]{article}

%===================================================================
\input{common_latex/common_latex_packages}
\input{common_latex/common_latex_definitions}
\input{common_latex/common_latex_help_format}
%===================================================================

\begin{document}

\begin{labcover}
\labtitle{Help Topic: MOOS-IvP String Parsing Utilities}
\vspace{0.1in}
\labdate{Spring 2020}
\vspace{0.1in}
\labauth{Michael Benjamin, mikerb@mit.edu}
\labauth{Department of Mechanical Engineering}
\labauth{MIT, Cambridge MA 02139}
\labline
\end{labcover}

\wikidoc{file=Help.MIPStringParse}
\wikidoc{auth=mikerb@mit.edu}

%======================================================================
\section*{MOOS-IvP String Parsing Utilities}
%======================================================================
\label{sec_moosivp_string_parse_utils}

The below describe a set of string utilities in the \var{MBUtils}
library distributed with MOOS-IvP. To use them, add \var{\#include
"MBUtils.h"} in your source code, and add the \var{mbutil} library to
the list of libraries your code links to, likely in your
local \var{CMakeLists.txt} file.

%======================================================================
\subsection*{The \func{biteString()} function}

The \func{biteString()} function takes a string and returns everything
to the left of the given character. The original string is modified and is
everything to the right of the character. 

\vspace{0.08in}
\begin{fileverb}
 string biteString(string, char);
\end{fileverb}

\vspace{0.1in}
\noindent
For example:

\vspace{0.08in}
\begin{fileverb}
 string orig = "temperature = 98";
 string left = biteString(orig, '=');
 cout &lt;&lt; "left: [" &lt;&lt; left &lt;&lt; "]" &lt;&lt; endl;
 cout &lt;&lt; "orig: [" &lt;&lt; orig &lt;&lt; "]" &lt;&lt; endl;
\end{fileverb}

\vspace{0.1in}
\noindent
Produces:

\vspace{0.08in}
\begin{consoleverb}
 left: [temperature ]
 orgi: [ 98];
\end{consoleverb}

\noindent
Notice that the white space to the left and the right of the '=' character
are preserved in the result. If your desire is to have these removed, you can 
invoke \func{biteStringX()} instead, described below. 

%======================================================================
\subsection*{The \func{biteStringX()} function}

The \func{biteStringX()} function does the same thing as the \func{biteString}
function but takes the additional step of removing blanks from the ends of the 
results.

\vspace{0.08in}
\begin{fileverb}
 string biteStringX(string, char);
\end{fileverb}

\vspace{0.1in}
\noindent
For example:

\vspace{0.08in}
\begin{fileverb}
 string orig  = "temperature = 98";
 string left = biteStringX(orig, '=');
 cout &lt;&lt; "left: [" &lt;&lt; left &lt;&lt; "]" &lt;&lt; endl;
 cout &lt;&lt; "orig: [" &lt;&lt; orig &lt;&lt; "]" &lt;&lt; endl;
\end{fileverb}

\vspace{0.1in}
\noindent
Produces:

\vspace{0.08in}
\begin{consoleverb}
 left: [temperature]
 orgi: [98];
\end{consoleverb}


%======================================================================
\subsection*{The \func{parseString()} function}

The \func{parseString()} function takes a string, and a character. It 
returns a vector of strings where each string is component of the original
string separated by the given character.

\vspace{0.08in}
\begin{fileverb}
 vector&lt;string&gt; parseString(string, char);
\end{fileverb}

\vspace{0.1in}
\noindent
For example:

\vspace{0.08in}
\begin{fileverb}
 string orig = "temperature=98, height=72, weight=150";
 vector&lt;string&gt; str_vector = parseString(orig, ',');
 for(unsigned int i=0; i&lt;str_vector.size(); i++)
   cout &lt;&lt; "component: [" &lt;&lt; str_vector[i] &lt;&lt; "]" &lt;&lt; endl;
\end{fileverb}

\vspace{0.1in}
\noindent
Produces:

\vspace{0.08in}
\begin{consoleverb}
  component = [temperature=98]
  component = [ height=72]
  component = [ weight=150]
\end{consoleverb}

\noindent
Notice that the white space to the left and the right of the ',' character
are preserved in the result. 

%======================================================================
\subsection*{The \func{parseStringQ()} function}

The \func{parseStringQ()} works like \func{parseString()} except that the 
character separated is ignored if it is enounctered between double-quotes.

\vspace{0.08in}
\begin{fileverb}
 vector&lt;string&gt; parseStringQ(string, char);
\end{fileverb}

\vspace{0.1in}
\noindent
For example:

\vspace{0.08in}
\begin{fileverb}
 string orig = "children="john,bob,mary", height=72, weight=150";
 vector&lt;string&gt; str_vector = parseStringQ(orig, ',');
 for(unsigned int i=0; i&lt;str_vector.size(); i++)
   cout &lt;&lt; "component: [" &lt;&lt; str_vector[i] &lt;&lt; "]" &lt;&lt; endl;
\end{fileverb}

\vspace{0.1in}
\noindent
Produces:

\vspace{0.08in}
\begin{consoleverb}
  component = [children=john,bob,mary]
  component = [ height=72]
  component = [ weight=150]
\end{consoleverb}

\noindent
Notice that the white space to the left and the right of the ',' character
are preserved in the result. 


%======================================================================
\subsection*{The \func{tokStringParse()} function}

The \func{tokStringParse()} works on a comma-separated list of
parameter=value pairs and pulls out the value for a given
parameter. The first character argument is the "global" separator, and
the second argument is the "local" separator.

\vspace{0.08in}
\begin{fileverb}
 string tokStringParse(string, string, char, char);
\end{fileverb}

\vspace{0.1in}
\noindent
For example:

\vspace{0.08in}
\begin{fileverb}
 string orig = "temperature=98.1, height=72, weight=150";

 string a = tokStringParse(orig, "temperature", ',', '=');
 string b = tokStringParse(orig, "height", ',', '=');
 string c = tokStringParse(orig, "weight", ',', '=');
 string d = tokStringParse(orig, "age", ',', '=');

 cout &lt;&lt; "a: [" &lt;&lt; a &lt;&lt; "]" &lt;&lt; endl;
 cout &lt;&lt; "b: [" &lt;&lt; b &lt;&lt; "]" &lt;&lt; endl;
 cout &lt;&lt; "c: [" &lt;&lt; c &lt;&lt; "]" &lt;&lt; endl;
 cout &lt;&lt; "d: [" &lt;&lt; d &lt;&lt; "]" &lt;&lt; endl;
\end{fileverb}

\vspace{0.1in}
\noindent
Produces:

\vspace{0.08in}
\begin{consoleverb}
  a: [98.1]
  b: [72]
  a: [150]
  a: []
\end{consoleverb}

\noindent
This function will also strip leading and trailing white space on its
return value.

\end{document}

</pre></body></html>