IAN LANG ELECTRONICS

So, let's have a trot through that sketch and work out what's happening. If we are going to measure a string, the first thing we need to have is a string to measure, so our first job is to declare one:

String STRINGA ="Matthew,Mark,Luke,John";

and since we are using the serial monitor we need to set up for serial comms, which we do in the setup:

void setup(){

Serial.begin(9600);

}

We set up for a baud rate of 9600 and so make sure your serial monitor has the same rate- you'll find the rate setter in the bottom left hand corner of your IDE.

On to the loop then and the first thing we see is;

void loop(){

Serial.print("There are ");Serial.print(STRINGA.length());Serial.print(" characters in the string:");

Serial.print is the command that sends out to the serial monitor as we know, however what we have never discussed hitherto in these pages is measuring the length of a string which we do with STRINGA.length(

STRINGA being the string we declared at the beginning. The whole thing, when done, gives a sentence that says:

There are 22 characters in the string:

or however many characters there are if you've changed the string. The next thing is to move down a line:

Serial.println();

and then begin printing out the characters;

for (int t=1;t<STRINGA.length()+1;t++){

So, in the above, we set up a for/next loop to go from 1 to whatever the number of characters in the string we have are. Now we can print:

Serial.println(STRINGA.substring(0,t));delay(1000); }

The substring command will print out characters from the left to the right and stop according to the value of t. The last thing we need to do is introduce a new line when the loop is finished:

Serial.println();

}

And that's it. Do you want it to print out more quickly, one character at a time but on the same line? Here's the modified sketch:

String STRINGA ="Matthew,Mark,Luke,John";

void setup(){

Serial.begin(9600);

}

void loop(){

Serial.print("There are ");Serial.print(STRINGA.length());Serial.print(" characters in the string:");

Serial.println();

for (int t=1;t<STRINGA.length()+1;t++){

Serial.print(STRINGA.substring(t-1,t));delay(200); }

Serial.println();

}

The modification comes in the line

Serial.print(STRINGA.substring(t-1,t));delay(200); }

in which the parameters have been set to t-1 and t which are the same character (as it goes to the one immediately after in both cases it prints character t). The delay has also been shortened to 1/5th of a second giving a kind of typewriter printout effect.

So, let's develop this a bit and find a way to sort out delimited strings.

What, you may ask, is a delimited string?

A delimited string, I answer, is a string of words in which each word has been delimited by a control character. Such a string is below:

String STRINGA ="Matthew~Mark~Luke~John~";

You'll see that the string has been split into four names (the Gospel writers if you were wondering) and between the names is a tilde ( ~ ) which I chose as being the least likely punctuation mark to appear anywhere because let's face it when was the last time you used a tilde? Here's the sketch to copy, paste and upload:

String STRINGA ="Matthew~Mark~Luke~John~";

int tildecount=0;

int thiswordchar=0;

void setup(){

Serial.begin(9600);

}

void loop(){

Serial.println();

Serial.print("There are ");Serial.print(STRINGA.length());Serial.print(" characters in the string:");

Serial.println();

for (int t=1;t<STRINGA.length()+1;t++){

if (STRINGA.substring(t-1,t)!="~"){

Serial.print(STRINGA.substring(t-1,t));thiswordchar++;delay(200); }

else{

delay(1000);

Serial.print("  which has ");Serial.print(thiswordchar);Serial.print(" characters");delay(1000);

Serial.println();tildecount++;thiswordchar=0;}

}

Serial.print(" The count of tildes was ");Serial.print(tildecount);tildecount=0;

delay(2000);Serial.println();

}

When you open your serial monitor it will tell you how many characters there are in the string, and print out each name on a seperate line and tell you how many characters there are in it. When it's done, it will tell you how many tildes it counted too. Over the page we'll have a canter through the code.

# Handling Strings on your Arduino

Go Back Previous Page More>