Evo C++ Library v0.5.1
String Parsing

This shows different approaches for string parsing.

Whitespace

Many Evo methods skip or strip whitespace. This may or may not include newlines – this will be noted in the method documentation.

Check Null or Empty
#include <evo/string.h>
using namespace evo;
int main() {
String str;
// Null string
if (str.null())
{ }
// Null string is also considered empty
if (str.empty())
{ }
// Non-empty string
if (!str.empty())
{ }
return 0;
}
Splitting

Splitting to substrings:

#include <evo/string.h>
using namespace evo;
int main() {
String str("foo=bar"), left, right;
// Split to left/right on first occurrence of '='
str.split('=', left, right);
// Split to just left
str.split('=', left);
// Split to just right
str.split('=', vNULL, right);
return 0;
}

Splitting to a list with generic conversion:

#include <evo/string.h>
#include <evo/strtok.h>
using namespace evo;
int main() {
String str("1,2,3");
// Split into list
List<Int> nums;
str.split<StrTok>(nums);
// Join back into list
nums.set();
str.join(nums); // set back to: 1,2,3
return 0;
}

Splitting to a list with generic conversion and alternate tokenizer:

#include <evo/string.h>
#include <evo/strtok.h>
using namespace evo;
int main() {
String str("1,'2,2',3");
// Split into list, with tokenizer supporting quoting
str.split<StrTokQ>(nums);
// Join back into list, with quoting
nums.set();
str.joinq(nums); // set back to: 1,'2,2',3
return 0;
}
Tokenizing

String and SubString have methods for extracting tokens:

#include <evo/substring.h>
#include <evo/io.h>
using namespace evo;
static Console& c = con();
int main() {
SubString str("one,two,three"), value;
while (str.token(value, ','))
c.out << value << NL;
return 0;
}

StrTok classes can parse into substring tokens:

#include <evo/substring.h>
#include <evo/strtok.h>
#include <evo/io.h>
using namespace evo;
static Console& c = con();
int main() {
// Tokenize in order (left to right)
StrTok tok1("one,two,three");
while (tok1.next(','))
c.out << tok1.value() << NL;
// Tokenize in reverse (right to left)
StrTokR tok2("one,two,three");
while (tok2.next(','))
c.out << tok2.value() << NL;
// Tokenize with quoting -- using StrTokQ
StrTokQ tok3("one,'two,two',three");
while (tok3.next(','))
c.out << tok3.value() << NL;
// Tokenize with quoting -- using nextq()
StrTok tok4("one,'two,two',three");
while (tok4.nextq(','))
c.out << tok4.value() << NL;
return 0;
}
Quoting

StrTokQ and StrTok::nextq() can parse quoted tokens.

See Smart Quoting

Low-Level Scanning