Hi everybody,
I have started to write demo-codes for the SafeString-library. My main goal is to write easy to understand demo-codes through beeing stripped down to a short example and a version with quite a lot comments and additional serial output to explain what is going on.
So here are two demo-codes.
This one is tripped down to the bare minimum to show how to use the function .stoken
#include "SafeString.h"
createSafeString(MyLongStr, 64);
createSafeString(MySmallerStr, 16);
int MyTokenNr = 0;
char MyDelimiter;
void setup() {
Serial.begin(115200);
Serial.println("Setup-Start");
MyDelimiter = ',';
MyLongStr = "Part 1 ,This is Part 2,that's Part 3 , and Part 4 ";
Serial.println(MyLongStr);
Serial.println("Start extracting...");
while (MyTokenNr >= 0) {
MyTokenNr = MyLongStr.stoken(MySmallerStr, MyTokenNr, MyDelimiter);
Serial.print("extracted a token ");
Serial.println(MySmallerStr);
}
Serial.println("finished extracting");
MyDelimiter = '@';
Serial.println(MyLongStr);
Serial.println("Start extracting...");
while (MyTokenNr >= 0) {
MyTokenNr = MyLongStr.stoken(MySmallerStr, MyTokenNr, MyDelimiter);
Serial.print("extracted a token ");
Serial.println(MySmallerStr);
}
Serial.println("finished extracting");
}
void loop() {
}
serial output should look like this
Setup-Start
Part 1 ,This is Part 2,that's Part 3 , and Part 4
Start extracting...
extracted a token Part 1
extracted a token This is Part 2
extracted a token that's Part 3
extracted a token and Part 4
finished extracting
Part 1 ,This is Part 2,that's Part 3 , and Part 4
Start extracting...
finished extracting
and here is a richly commented version that explains a lot of details and has some additional functions
#include "SafeString.h"
// defining variables of type SafeString is a little bit different from defining
// standard variable definitions. It is similar to a function-call
// the basic structure is
// createSafeString( <Name-of-Variable>, <Number-of-max-characters>);
createSafeString(MyLongStr, 64);
createSafeString(MySmallerStr, 16);
int MyTokenNr = 0;
char MyDelimiter;
// parameters of variable-type SafeString
// need a trailing '&'
// |
void PrintlnDblX(SafeString& p_ChToPrint) {
//code should always be structured into functions where each function
// does ONE thing
// this functions prints a leading '#' and a trailing '#'
// for clearly indicating what characters are inside a string
Serial.print("#");
Serial.print(p_ChToPrint);
Serial.println("#");
}
void setup() {
Serial.begin(115200);
Serial.println("Setup-Start");
}
void loop() {
TokenizeAtCommas();
TryTo_TokenizeAtCommas();
while(true); // empty loop. Effect: stops loop from looping
}
void TokenizeAtCommas() {
MyDelimiter = ',';
// the String below contains four "tokens"
// token just means a sequence of characters until a delimiting character
// delimiting character in this example is comma ','
// so the string below has the tokens
// "Part 1 "
// "This is Part 2"
// "that's Part 3 "
// " and Part 4 "
// pay attention to the leading and the trailing spaces
MyLongStr = "Part 1 ,This is Part 2,that's Part 3 , and Part 4 ";
Serial.print("Tokenizing string at delimiting character ");
Serial.println(MyDelimiter);
Serial.println();
Serial.println("String to tokenize is");
PrintlnDblX(MyLongStr);
// let's "tokenize" the string
// as long as there are tokens found the "stoken"-function returns
// a value > 0. If no more tokens were found it returns -1
Serial.println("start extracting...");
while (MyTokenNr >= 0) {
MyTokenNr = MyLongStr.stoken(MySmallerStr, MyTokenNr, MyDelimiter);
Serial.print("extracted a token ");
PrintlnDblX(MySmallerStr);
}
Serial.println("extracting finished");
Serial.println(); Serial.println(); Serial.println();
}
void TryTo_TokenizeAtCommas() {
// delimiter ist set to '@'
// string still has commas ',' => extracting tokens will not work
// due to the wrong delimiter-character
MyDelimiter = '@';
MyLongStr = "Part 1 ,This is Part 2,that's Part 3 , and Part 4 ";
Serial.print("Tokenizing string at delimiting character '");
Serial.print(MyDelimiter);
Serial.println("'");
Serial.println();
Serial.println("String to tokenize is");
PrintlnDblX(MyLongStr);
// let's "tokenize" the string
// as long as there are tokens found the "stoken"-function returns
// a value > 0. If no more tokens were found it returns -1
Serial.println("start extracting...");
while (MyTokenNr >= 0) {
MyTokenNr = MyLongStr.stoken(MySmallerStr, MyTokenNr, MyDelimiter);
Serial.print("extracted a token ");
PrintlnDblX(MySmallerStr);
}
Serial.println("extracting finished");
Serial.println(); Serial.println(); Serial.println();
}
serial output should look like this
Setup-Start
Tokenizing string at delimiting character ,
String to tokenize is
#Part 1 ,This is Part 2,that's Part 3 , and Part 4 #
start extracting...
extracted a token #Part 1 #
extracted a token #This is Part 2#
extracted a token #that's Part 3 #
extracted a token # and Part 4 #
extracting finished
Tokenizing string at delimiting character '@'
String to tokenize is
#Part 1 ,This is Part 2,that's Part 3 , and Part 4 #
start extracting...
extracting finished
As I'm learning how to use the SafeString-library myself from time to time I will add more democodes that explain other functions of SafeString.
best regards Stefan