String Manipulation

ROBOTC provide native support for string variables. This is an extension beyond the normal capabilities found in the ‘C’ language; in standard ‘C’ strings are manipulated with arrays of ‘char’.

String variables can contain up to 20 characters; 20 characters was chosen because it is more characters than will fit on one line of the NXT display.

Native support means that normal “opcodes” apply to strings. In particular, you can:

string stringA;                 // Define a string variable

string stringB = “Hello World”; // Define a string variable with an initial value

string stringD[10];             // Define a string array of 10 elements.

stringA = stringB;

stringA = “Hello World”;

stringA  = stringB + stringC;  // stringA           is the concatenated result of ‘stringb’ with ‘stringB’

stringA += stringB;            // stringA           is added to the end of stringA

stringA += “More Characters”;  // “More Characters” is added to the end of stringA

if (stringA == stringB) . . .

if (stringA >= stringB) . . .

if (stringA <  stringB) . . .

etc

The following functions also apply to string variables. More detailed technical information is at the end of the document.

 

StringDelete(sDest, nIndex, nSize);

Deletes a substring from a string

 

StringFind(sDest, nIndex, sSearch);

Finds the position in a string of the selected substring

 

StringFormat(sDest, sFmtSpec, nParm1, nParm2);

Formats a string using the specified format string and up to two parameters for formating

 

strLen(sString)

Functions returns the size of a string

 

 

 

Strings and ROBOTC

A ‘string’ is an array of characters terminated with a ‘null’ (zero value) character. In the ideal solution, you’d be able to manipulate strings just like numeric variables as shown in the following examples.

string sVar1;                // Declare ‘string’ variable

string sVar2;

 

sVar1 = “Hello”;             // Assign ‘sVar1’ the value “Hello”

sVar1 = sVar2;               // Copy (Assign) the value of string ‘sVar2’ to ‘sVar1’         

 

sVar1 = “Hello” + sVar2;    // Concatenate “Hello” and ‘sVar2’ into a

                             // single string and assign the results to

                             // ‘sVar1’

 

if (sVar1 == sVar2) . . .    // Comparison of two strings

if (sVar1 <= sVar2) . . .    // Comparison of two strings

if (sVar1 <  sVar2) . . .    // Comparison of two strings

 

C’ String Support

char sVar1[50];              // User must explicitly declare size of

                             // array. The dimension 50 is arbitrary.

char sVar2[50];

 

strcpy(sVar1, “Hello”);      // Assign ‘sVar1’ the value “Hello” using

                             // the string copy (strcpy) function.

strcpy(sVar1, sVar2);        // Copy (Assign) the the value of string

                             // of ‘sVar2’ to ‘sVar1’         

 

strcat(sVar1, “Hello”);      // Concatenate “Hello” and ‘sVar2’ into a

strcat(sVar1, sVar2);        // single string and assign the results to

                             // ‘sVar1’

 

if (strcmp(sVar1, sVar2) == 0)// Comparison (==)of two strings

if (strcmp(sVar1, sVar2) < 0) // Comparison (<) of two strings

if (strcmp(sVar1, sVar2) >  0)// Comparison (>) of two strings

It is the responsibility of the application programmer to ensure that the size of the char array (50 in this case) is large enough to hold the result.  There is no error checking, for example, if you try to assign a 55-character string to a 50-element char array!

 

strcat(pToBuffer, pFromBuffer)     

                                       Function concatenates 'pFromBuffer' onto end of 'pToBuffer'. The variables are arrays of bytes. It is user responsibility to ensure that the 'To' array is large enough to hold the result. ROBOTC is not able to do any range checking!

 

strcmp(pString1, pString2)   

                                       Function compares 'pString1' with 'pString2'. Returns negative value if less than, 0 if equal and positive value if greater than. The variables are arrays of bytes terminated with a zero char.

 

strcpy(pToBuffer, pFromBuffer)     

                                       Function copies 'pFromBuffer' to 'pToBuffer'. The variables are arrays of bytes terminated with a zero character. It is user responsibility to ensure that the 'To' array is large enough to hold the result. ROBOTC is not able to do any range checking!

 

strncat(pToBuffer, pFromBuffer, nMaxBufferSize)

                                       Function concatenates 'pFromBuffer' onto end of 'pToBuffer'. The variables are arrays of bytes terminated with a zero character. nMaxBufferSize is the maximum size of ‘pFromBuffer’ and is usually created with a ‘sizeof(..)’ function call.

 

strncmp(pString1, pString2, nMaxBufferSize)    

                                       Function compares 'pString1' with 'pString2'. Returns negative value if less than, 0 if equal and positive value if greater than. The variables are arrays of bytes terminated with a zero char. ‘nMaxBufferSize’ is the maximum number of bytes to compare and is usually created with a ‘sizeof(..)’ function call.

 

strncpy(pToBuffer, pFromBuffer, nMaxBufferSize)

                                       Function copies 'pFromBuffer' to 'pToBuffer'. The variables are arrays of bytes terminated with a zero character. It is user responsibility to ensure that the 'pToBuffer' array is large enough to hold the result. ‘nMaxBufferSize ‘is the maximum size of ‘pFromBuffer’ and is usually created with a ‘sizeof(..)’ function call.  

 

The above functions are identical to the functions found in conventional C 'string.h' library.

 ‘‘C++’ String Support

C++ systems usually define a string ‘class’ variable that provides rich support for strings. This class will provide all of the functionality of the examples shown above and more. The class typically provides dynamic allocation of the memory needed by a string so that the class can efficiently support strings of any size. The class implementation will have range checking on string size and protect against the possibility of overwriting outside of the bounds of the string variable.

ROBOTC String Support

ROBOTC’s objectives include ease of use and compatibility with the ‘C’ language. With string manipulation, the decision was made to avoid the use of ‘char’ arrays and provide native support for a string variable type within the ROBOTC language. This does deviate from ‘C’ but provides an easier to use and more robust solution.

‘string’ variables in ROBOTC (on the NXT platform) are always allocated 20 ‘char’. This size was chosen as it is larger than the biggest internal string variable found in the firmware – i.e. filenames can be up to 19 characters long – and is also larger that the amount of text that can be displayed on a single line of the NXT LCD.

The syntax and usage of string variables is exactly as shown in the examples at the top of this document. Specifically,

string sVar1;                // Declare ‘string’ variable

string sVar2;

 

sVar1 = “Hello”;             // Assign ‘sVar1’ the value “Hello”

sVar1 = sVar2;               // Copy (Assign) the the value of string

                             // of ‘sVar2’ to ‘sVar1’          

 

sVar1  = “Hello” + sVar2;    // Concatenate “Hello” and ‘sVar2’ into a

                             // single string and assign the results to

                             // ‘sVar1’

 

if (sVar1 == sVar2) . . .    // Comparison of two strings

if (sVar1 <= sVar2) . . .    // Comparison of two strings

if (sVar1 <  sVar2) . . .    // Comparison of two strings

In addition, ROBOTC provides the following additional native support for string variables.

string sVar;   // Declare ‘string’ variable

string sVar2;  // Declare ‘string’ variable

byte   bVar;

int    iVar;

float  fVar;

 

sVar = bVar;  // Assign single char ‘bVar’ to ‘sVar’

sVar = iVar;  // Convert ‘iVar’ to a text string and assign to ‘sVar’

sVar = fVar;  // Convert ‘iVar’ to a text string and assign to ‘sVar’

 

// Convert numeric variables to text and concatenate to end of string

 

sVar += bVar; 

sVar += iVar; 

sVar += fVar; 

 

// Miscellaneous Functions

 

strlen(sVar)      // Function returns the current length of string

StringDelete(..)  // Deletes characters within a string

StringFind(..)    // Finds substring within a string

StringFormat(..)  // Formats a string