General/Strings

From ROBOTC API Guide
< General
Revision as of 06:07, 21 May 2012 by Bfeher (Talk | contribs)

Jump to: navigation, search


For information about displaying on the NXT LCD, please see the article: NXT Display Functions.

For information about displaying on the VEX LCD, please see the article: Cortex Display Functions.


When displaying floats, for example, you can tell ROBOTC how many decimals places to display. This is standard across all 'C' - like programing languages. For example, if your float is PI (3.14159265), but you only want to dispay "3.14", your string should contain, "%1.2f".

The number before the decimal is how many digits before the decimal you wish to display, while the number after the decimal is how many digits after the decimal you wish to display. So "%1.2f" tells us to display one digit before the decimal and two digits after the decimal, with "3.14" as the final result.


Color Key
Function:
Variable:


sprintf

void sprintf ( string &sString , const string sFormatString , ... )

(void) This function writes the data from sFormatString to &sString. Identical to the function found in conventional C 'string.h' library.
Parameter Explanation Data Type
&sString This is the destination string. void
sFormatString, ... These are the source strings. void
task main()
{
  string srce = "ROBOTC"; // create source variable 'srce' and set it to "ROBOTC"
  string dest;            // create destination variable 'dest'
 
  wait1Msec(1000);        // wait 1 second
 
  sprintf(dest, srce);    // write to 'dest', the contents of 'srce' (dest <- "ROBOTC)
 
  while(true);            // infinite loop (so we can watch the debbugger)
}



strcat

void strcat ( void &pToBuffer , const void &pFromBuffer )

(void) Function concatenates pFromBuffer onto end of pToBuffer. The variables are arrays of bytes terminated with 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! Identical to the function found in conventional C 'string.h' library.
Parameter Explanation Data Type
pToBuffer Characters from 'pFromBuffer' are concatenated to the end of this. void
pFromBuffer Concatenates characters from here. void
string str1 = "ROBOT";       // string 'str1' is "ROBOT"
string str2 = "C";           // string 'str2' is "C"
 
strcat(str1, str2);          // concatinate string 'str2' onto string 'str1'
 
while(true);  // infinite loop (to keep debugger running)


strcmp

short strcmp ( void &pString1 , const void &pString2 )

(short) 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. Identical to the function found in conventional C 'string.h' library.
Parameter Explanation Data Type
pString1 A string to compare with 'pString2'. void
pString2 A string to compare with 'pString1'. void
string str1 = "Fett";                       // string 'str1' is "Fett"
string str2 = "Fett";                       // string 'str2' is "Fett"
 
if(strcmp(str1, str2) < 0)                    // if 'str1' < 'str2':
{
  // 'str1' is < 'str2'
}
else if(strcmp(str1, str2) > 0)               // if 'str1' > 'str2':
{ 
  // 'str1' is > 'str2'
}
else                                          // if 'str1' == 'str2':
{
  // 'str1' is = 'str2'
}


strcpy

void strcpy ( void &pToBuffer , const void &pFromBuffer )

(void) 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! Identical to the function found in conventional C 'string.h' library.
Parameter Explanation Data Type
pToBuffer Copies 'pFromBuffer' to this. void
pFromBuffer Copies this to 'pToBuffer'. void
string str1 = "";            // 'str1' is "" (empty)
string str2 = "DNA";         // string 'str2' is "DNA"
 
strcpy(str1, str2);          // copy string 'str2' onto string 'str1'
 
while(true);  // infinite loop (to keep debugger running)


StringDelete

void StringDelete ( string &sDest , const int nIndex , const int nSize )

(void) Deletes a substring from a string.
Parameter Explanation Data Type
sDest The string to delete from. string
nIndex The index of the first character to delete (1st character = index 0). int
nSize The number of characters to delete, starting 'nIndex'. int
string str = "mesGARBAGEsage";        // String 'str' is "mesGARBAGEsage".
 
StringDelete(str, 3, 7);              // Delete from index 3 to the 7th char after that.
                                      /* Confusing because 3 = index 3, not char 3.  Since indexing */
                                      /* starts with 0, index 3 is the 4th character.               */
 
while(true);  // infinite loop (to keep debugger running)


StringFind

int StringFind ( const string &sSrce , const string &sSearch )

(int) Finds the position in a string of the selected substring.
Parameter Explanation Data Type
sSrce String to search through. string
sSearch Substring to look for. string
string str = "mesHIDDENsage";                     // String 'str' is "mesGARBAGEsage".
int index_of_substr;                              // Int, 'index_of_substr' to be used by "StringFind()".
 
index_of_substr = StringFind(str_msg, "HIDDEN");  /* Search for the substring, "HIDDEN" */
                                                  /* withing the string, 'str'.         */
 
while(true);  // infinite loop (to keep debugger running)


StringFormat

void StringFormat ( string &sDest , const string sFormatString , ... )

(void) Formats a string using the specified format-string.
Parameter Explanation Data Type
sDest The string to format. string
sFormatString The format-string to apply to 'sDest'. string
string str = "";                  // Create String, 'str' and initialize it as 'empty'.
float num = 3.14159;              // Create float, 'num' and initialize it as first 6 digits of pi.
 
StringFormat(str, "%1.2f", num);  /* Format num into a float to 2 decimal places, then send it to
                                     the string, 'str'.                                          */
while(true);  // infinite loop (to keep debugger running)


StringFromChars

void StringFromChars ( string &sToString , const char &FromChars )

(void) Converts an array of bytes to a string value. You MUST end your char array with a char value of zero!
Parameter Explanation Data Type
sToString The string to copy characters to. string
FromChars The character array to copy characters from. char
char test[10];    // create an array of chars of size 10 named 'test'
string str = "";  // create an empty string named 'str'
 
test[0] = 'R';
test[1] = 'O';
test[2] = 'B';
test[3] = 'O';
test[4] = 'T';
test[5] = 'C';
test[6] = (char)0;  // index 6 is character value zero
test[7] = 'X';      /* these characters               */
test[8] = 'X';      /* never get copied               */
test[9] = 'X';      /* since they are after the zero  */
 
StringFromChars(str, test);  // copy chars from 'test' to 'str'
 
while(true);  // infinite loop (to keep debugger running)
}


strncat

void strncat ( void &pToBuffer , const void &pFromBuffer , const short nMaxBufferSize )

(void) 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. Identical to the function found in conventional C 'string.h' library file.
Parameter Explanation Data Type
pToBuffer 'pFromBuffer' gets concatenated onto the end of this. void
pFromBuffer This gets concatenated onto the end of 'pToBuffer'. void
nMaxBufferSize How many characters to concatenate. short
string str1 = "ROBOT";             // String 'str1' is "ROBOT".
string str2 = "C!";                // String 'str2' is "C!".
 
int copy_length = 1;               // Int 'copy_length' is set to 1.
 
strncat(str1, str2, copy_length);  /* Concatinate 'copy_length' amount of characters 
                                      from string 'str2' onto string 'str1'.         */
 
while(true);  // infinite loop (to keep debugger running)


strncmp

short strncmp ( void &pString1 , const void &pString2, const short nMaxBufferSize )

(short) 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. Identical to the function found in conventional C 'string.h'
Parameter Explanation Data Type
pString1 A string to compare with 'pString2'. void
pString2 A string to compare with 'pString1'. void
nMaxBufferSize How many characters to compare, starting from index 0. short
string str1 = "Fett";                       // String 'str1' is "Fett".
string str2 = "Fetts";                      // String 'str2' is "Fetts".
 
int cmp_chars = 4;                          /* Int 'cmp_chars' is the amount of chars 
                                               to compare in "strncmp()".  Here we will 
                                               compare the first 4 chars of each string. */
 
if(strncmp(str1, str2, cmp_chars) < 0)        // If 'str1' < 'str2' up to 4 chars:
{
  // 'str1' is < 'str2' up to 4 chars.
}
else if(strncmp(str1, str2, cmp_chars) > 0)   // If 'str1' > 'str2' up to 4 chars:
{ 
  // 'str1' is > 'str2' up to 4 chars.
}
else                                          // If 'str1' == 'str2' up to 4 chars:
{
  // 'str1' is = 'str2' up to 4 chars.
}