WString.h Documentation

For the past few months I have been working with the Teensy3 chips from PJRC.COM. I love them like I would a member of my family. They are fast and moving to them has made my life much simpler. However, most of the time I like to do string manipulations and I have chosen the WString.h library.  However, every time I want to use it I have to go and look at the header file to work out what functions it has. So I thought I would write a quick blog post on the topic here rather than on StackOverflow.com as I have no specific question to answer.

So, when programming the Teensy3 using the Arduino.cc Wiring/C++ (ish) language  you should do the following:

BASIC USE

#include <WString.h>

void setup() {

  int anInteger = 20;

  String aString = String(“Some text”) + String(“, Some other text: “) + String(anInteger, DEC);

  Serial.println(aString);

}

void loop() {

}

Will output the following:

Some text, Some other text: 20

NUMBER FORMATTING

You can convert numbers into strings using some of their inbuilt functions.  As you can see above I have printed an integer into it’s decimal representation. You are also able to place any base where I have the DEC which is a #define 10 somewhere else in the compile chain. HEX is also another one you can use… or you can just replace those tokens with the actual numbers 10 and 16. Or perhaps you want Octal, you can use 8.

If you want to look at the WString.h file at google code you can see these constructors at the beginning of the header file.

STRING METHODS

String aString = String(“0123456789”);

String a = aString.substring(0, 4);  // produces 0123

String b = aString.substring(3);  // produces 3456789

String c = aString.charAt(2); // produces 2

SEARCHING STRINGS

if( aString.indexOf(F(“4”)) >= 0) {

Serial.println(“True”);

}

STRING MANIPULATION

String uString = String(“This is a Test”);
Serial.println(uString.toLowerCase());  // produces this is a test

Serial.println(uString.toUpperCase() ); // produces THIS IS A TEST

Serial.println(uString.trim() ) ; // removes leading and trailing white space

Serial.println(uString.setCharAt(2, ‘T’)); // produces: ThTs is a Test

That should be enough to get people started. The only other function I use often is the toCharArray() function. Some functions such as sscanf.  Here is an example of how to convert a String into it’s integer components.  Handy when passing data in from a file or some device.

String s = String(“Tuple: 1, 2, 3, 4, 5, 6”);
char str[80];
s.toCharArray(str, s.length() + 1, 0);
int a, b, c, d, e, f;
sscanf(str, “Tuple: %d, %d, %d, %d, %d, %d”, &a, &b, &c, &d, &e, &f);
Serial.println(a, DEC);

Advertisements

About slushfundsupervisor

I work in the tech industry and have done for a long time. So, I have opinions. I like to share them.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s