Difference between revisions of "General/Strings"

From ROBOTC API Guide
Jump to: navigation, search
(strncat)
(strcat)
 
(10 intermediate revisions by one user not shown)
Line 1: Line 1:
{| style="font-family:Verdana, Genega, sans-sarif; font-size:80%;color:gray;" width="100%" cellpadding="0%" cellspacing="0" border="0"
+
<yambe:breadcrumb self="Strings">General|General Programming</yambe:breadcrumb>
|-
+
<br />
|
+
''[[Main_Page|Main]] >> [[VEX2_Main|CORTEX]] >> [[VEX2: Functions| Functions and Variables]] >> [[VEX2:_Functions_-_Strings|Strings]] ''
+
|-
+
|}
+
 
+
  
{| class="wikiText"
+
{|
 
|-
 
|-
|''For information about displaying on the VEX LCD, please see the article: [[VEX2:_Functions_-_Display|Display]].''
+
|''For information about displaying on the NXT LCD, please see the article: [[NXT_Functions_Display|NXT Display Functions]].''
 +
 
 +
''For information about displaying on the VEX LCD, please see the article: [[VEX2_Functions_Display|Cortex Display Functions]].''
  
  
Line 17: Line 14:
 
|-
 
|-
 
|}
 
|}
 +
<br />
  
 +
{{tl|1|1}}
 +
<br />
  
{|
+
== sprintf ==
 +
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
|style="vertical-align:top"| __TOC__
+
| class="functionType"|
|style="vertical-align:top"|
+
<span class="bigKeywordBI">void </span>
{| width="100" cellpadding="2" cellspacing="0" style="border-collapse: collapse; border-width: 1px; border-style: solid; border-color: #000"
+
<span class="bigKeywordB">sprintf</span>
!colspan="2" class="wikiHeader"|Color Key
+
<span class="bigCodePunc">(</span>
|-
+
<span class="bigKeywordBI">string </span>
|class="wikiText" width="75%" style="border-style: solid; border-width: 1px 0px 0px 0px"|Function:
+
<span class="bigCodeBasic">&sString</span>
|width="25%" style="border-style: solid; border-width: 1px 0px 0px 0px;" class="colorKeyFunc"|
+
<span class="bigCodePunc">, </span>
|-
+
<span class="bigKeywordBI">const string </span>
|class="wikiText" width="75%" style="border-style: solid; border-width: 0px 0px 0px 0px"|Variable:
+
<span class="bigCodeBasic">sFormatString</span>
|width="25%" style="border-style: solid; border-width: 0px 0px 0px 0px;" class="colorKeyVar"|
+
<span class="bigCodePunc">,  </span>
|}
+
<span class="bigCodeBasic">...</span>
 +
<span class="bigCodePunc">)</span>
 +
|-
 +
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) This function writes the data from <span class="codeBasic">sFormatString</span> to <span class="codeBasic">&sString</span>. Identical to the function found in conventional C 'string.h' library.
 +
|-
 +
|
 +
  {| class="parameterTable" cellpadding="5%" width="100%"
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter
 +
  ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type
 +
  |-
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|''&sString''
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|This is the destination string.
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|[[Data_Types#dataType_string|string]]
 +
  |-
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|''sFormatString, ...''
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|These are the source strings.
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|[[Data_Types#dataType_string|string]]
 +
  |-
 +
  |}
 +
|-
 +
|
 +
  {|
 +
  |-
 +
  |<syntaxhighlight lang="ROBOTC">
 +
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)
 +
}
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 
|-
 
|-
 
|}
 
|}
 +
<br />
  
 +
<!-- BROKEN
 +
== sprintf ==
 +
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 +
|-
 +
| class="functionType"|
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigKeywordB">sprintf</span>
 +
<span class="bigCodePunc">(</span>
 +
<span class="bigKeywordBI">string </span>
 +
<span class="bigCodeBasic">&sString</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const string </span>
 +
<span class="bigCodeBasic">sFormatString</span>
 +
<span class="bigCodePunc">, )</span>
 +
<span class="bigCodeBasic">...</span>
 +
<span class="bigCodePunc">)</span>
 +
|-
 +
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) This function writes the data from <span class="codeBasic">sFormatString</span> to <span class="codeBasic">&sString</span>. Identical to the function found in conventional C 'string.h' library.
 +
|-
 +
|
 +
  {| class="parameterTable" cellpadding="5%" width="100%"
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter
 +
  ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type
 +
  |-
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|''&sString''
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|This is the destination string.
 +
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|[[Data_Types#dataType_void|void]]
 +
  |-
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|''sFormatString, ...''
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|These are the source strings.
 +
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|[[Data_Types#dataType_void|void]]
 +
  |-
 +
  |}
 +
|-
 +
|
 +
  {|
 +
  |-
 +
  |<syntaxhighlight lang="ROBOTC">
 +
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)
 +
}
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 +
|-
 +
|}
 +
<br />
 +
-->
  
 
== strcat ==
 
== strcat ==
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
| class="functionType"| <span class="bigKeywordBI">void </span><span class="bigKeywordB">strcat</span><span class="bigCodePunc">(</span><span class="bigKeywordBI">void </span><span class="bigCodeBasic">&pToBuffer</span><span class="bigCodePunc">, </span><span class="bigKeywordBI">const void </span><span class="bigCodeBasic">&pFromBuffer</span><span class="bigCodePunc">)</span>
+
| class="functionType"|  
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigKeywordB">strcat</span>
 +
<span class="bigCodePunc">(</span>
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigCodeBasic">&pToBuffer</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const void </span>
 +
<span class="bigCodeBasic">&pFromBuffer</span>
 +
<span class="bigCodePunc">)</span>
 
|-
 
|-
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Function concatenates <span class="codeBasic">pFromBuffer</span> onto end of <span class="codeBasic">pToBuffer</span>. 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.  
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Function concatenates <span class="codeBasic">pFromBuffer</span> onto end of <span class="codeBasic">pToBuffer</span>. 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.  
Line 68: Line 174:
 
strcat(str1, str2);          // concatinate string 'str2' onto string 'str1'
 
strcat(str1, str2);          // concatinate string 'str2' onto string 'str1'
  
displayNextLCDString(str1);  // display the new 'str' ("ROBOTC")
+
while(true);  // infinite loop (to keep debugger running)
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-
Line 79: Line 185:
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
| class="functionType"| <span class="bigKeywordBI">short </span><span class="bigKeywordB">strcmp</span><span class="bigCodePunc">(</span><span class="bigKeywordBI">void </span><span class="bigCodeBasic">&pString1</span><span class="bigCodePunc">, </span><span class="bigKeywordBI">const void </span><span class="bigCodeBasic">&pString2</span><span class="bigCodePunc">)</span>
+
| class="functionType"|  
 +
<span class="bigKeywordBI">short </span>
 +
<span class="bigKeywordB">strcmp</span>
 +
<span class="bigCodePunc">(</span>
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigCodeBasic">&pString1</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const void </span>
 +
<span class="bigCodeBasic">&pString2</span>
 +
<span class="bigCodePunc">)</span>
 
|-
 
|-
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_short|short]]) Function compares <span class="codeBasic">pString1</span> with <span class="codeBasic">pString2</span>. 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.
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_short|short]]) Function compares <span class="codeBasic">pString1</span> with <span class="codeBasic">pString2</span>. 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.
Line 108: Line 223:
 
if(strcmp(str1, str2) < 0)                    // if 'str1' < 'str2':
 
if(strcmp(str1, str2) < 0)                    // if 'str1' < 'str2':
 
{
 
{
  displayNextLCDString("str1 is < than str2");   // display that 'str1' is < 'str2'
+
   // 'str1' is < 'str2'
 
}
 
}
 
else if(strcmp(str1, str2) > 0)              // if 'str1' > 'str2':
 
else if(strcmp(str1, str2) > 0)              // if 'str1' > 'str2':
 
{  
 
{  
   displayNextLCDString("str1 is > than str2");  // display that 'str1' is > 'str2'
+
   // 'str1' is > 'str2'
 
}
 
}
 
else                                          // if 'str1' == 'str2':
 
else                                          // if 'str1' == 'str2':
 
{
 
{
   displayNextLCDString("str1 = str2");          // display that 'str1' is = 'str2'
+
   // 'str1' is = 'str2'
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 128: Line 243:
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
| class="functionType"| <span class="bigKeywordBI">void </span><span class="bigKeywordB">strcpy</span><span class="bigCodePunc">(</span><span class="bigKeywordBI">void </span><span class="bigCodeBasic">&pToBuffer</span><span class="bigCodePunc">, </span><span class="bigKeywordBI">const void </span><span class="bigCodeBasic">&pFromBuffer</span><span class="bigCodePunc">)</span>
+
| class="functionType"|  
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigKeywordB">strcpy</span>
 +
<span class="bigCodePunc">(</span>
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigCodeBasic">&pToBuffer</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const void </span>
 +
<span class="bigCodeBasic">&pFromBuffer</span>
 +
<span class="bigCodePunc">)</span>
 
|-
 
|-
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Function copies <span class="codeBasic">pFromBuffer</span> to <span class="codeBasic">pToBuffer</span>. 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.
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Function copies <span class="codeBasic">pFromBuffer</span> to <span class="codeBasic">pToBuffer</span>. 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.
Line 157: Line 281:
 
strcpy(str1, str2);          // copy string 'str2' onto string 'str1'
 
strcpy(str1, str2);          // copy string 'str2' onto string 'str1'
  
displayNextLCDString(str1);  // display the new 'str' ("DNA")
+
while(true);  // infinite loop (to keep debugger running)
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-
Line 168: Line 292:
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
| class="functionType"| <span class="bigKeywordBI">void </span><span class="bigKeywordB">StringDelete</span><span class="bigCodePunc">(</span><span class="bigKeywordBI">string </span><span class="bigCodeBasic">&sDest</span><span class="bigCodePunc">, </span><span class="bigKeywordBI">const int </span><span class="bigCodeBasic">nIndex</span><span class="bigCodePunc">, </span><span class="bigKeywordBI">const int </span><span class="bigCodeBasic">nSize</span><span class="bigCodePunc">)</span>
+
| class="functionType"|  
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigKeywordB">StringDelete</span>
 +
<span class="bigCodePunc">(</span>
 +
<span class="bigKeywordBI">string </span>
 +
<span class="bigCodeBasic">&sDest</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const int </span>
 +
<span class="bigCodeBasic">nIndex</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const int </span>
 +
<span class="bigCodeBasic">nSize</span>
 +
<span class="bigCodePunc">)</span>
 
|-
 
|-
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Deletes a substring from a string.
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Deletes a substring from a string.
Line 202: Line 338:
 
                                       /* starts with 0, index 3 is the 4th character.              */
 
                                       /* starts with 0, index 3 is the 4th character.              */
 
   
 
   
displayNextLCDString(3, "%s", str1);  // Display the new 'str' ("message").
+
while(true);  // infinite loop (to keep debugger running)
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-
Line 213: Line 349:
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
| class="functionType"| <span class="bigKeywordBI">int </span><span class="bigKeywordB">StringFind</span><span class="bigCodePunc">(</span><span class="bigKeywordBI">const string </span><span class="bigCodeBasic">&sSrce</span><span class="bigCodePunc">, </span><span class="bigKeywordBI">const string </span><span class="bigCodeBasic">&sSearch</span><span class="bigCodePunc">)</span>
+
| class="functionType"|  
 +
<span class="bigKeywordBI">int </span>
 +
<span class="bigKeywordB">StringFind</span>
 +
<span class="bigCodePunc">(</span>
 +
<span class="bigKeywordBI">const string </span>
 +
<span class="bigCodeBasic">&sSrce</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const string </span>
 +
<span class="bigCodeBasic">&sSearch</span>
 +
<span class="bigCodePunc">)</span>
 
|-
 
|-
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_int|int]]) Finds the position in a string of the selected substring.
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_int|int]]) Finds the position in a string of the selected substring.
Line 243: Line 388:
 
                                                   /* withing the string, 'str'.        */
 
                                                   /* withing the string, 'str'.        */
 
   
 
   
displayNextLCDNumber(index_of_substr);           // Display the new index of the substring, "HIDDEN".
+
while(true); // infinite loop (to keep debugger running)
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-
Line 254: Line 399:
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
| class="functionType"| <span class="bigKeywordBI">void </span><span class="bigKeywordB">StringFormat</span><span class="bigCodePunc">(</span><span class="bigKeywordBI">string </span><span class="bigCodeBasic">&sDest</span><span class="bigCodePunc">, </span><span class="bigKeywordBI">const string </span><span class="bigCodeBasic">sFormatString</span><span class="bigCodePunc">, </span><span class="bigCodeBasic">...</span><span class="bigCodePunc">)</span>
+
| class="functionType"|  
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigKeywordB">StringFormat</span>
 +
<span class="bigCodePunc">(</span>
 +
<span class="bigKeywordBI">string </span>
 +
<span class="bigCodeBasic">&sDest</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const string </span>
 +
<span class="bigCodeBasic">sFormatString</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigCodeBasic">...</span>
 +
<span class="bigCodePunc">)</span>
 
|-
 
|-
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Formats a string using the specified format-string.
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Formats a string using the specified format-string.
Line 283: Line 439:
 
StringFormat(str, "%1.2f", num);  /* Format num into a float to 2 decimal places, then send it to
 
StringFormat(str, "%1.2f", num);  /* Format num into a float to 2 decimal places, then send it to
 
                                     the string, 'str'.                                          */
 
                                     the string, 'str'.                                          */
displayNextLCDString(str);       // Display the new string, str. ("3.14").
+
while(true); // infinite loop (to keep debugger running)
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-
Line 294: Line 450:
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
| class="functionType"| <span class="bigKeywordBI">void </span><span class="bigKeywordB">StringFromChars</span><span class="bigCodePunc">(</span><span class="bigKeywordBI">string </span><span class="bigCodeBasic">&sToString</span><span class="bigCodePunc">, </span><span class="bigKeywordBI">const char </span><span class="bigCodeBasic">&FromChars</span><span class="bigCodePunc">)</span>
+
| class="functionType"|  
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigKeywordB">StringFromChars</span>
 +
<span class="bigCodePunc">(</span>
 +
<span class="bigKeywordBI">string </span>
 +
<span class="bigCodeBasic">&sToString</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const char </span>
 +
<span class="bigCodeBasic">&FromChars</span>
 +
<span class="bigCodePunc">)</span>
 
|-
 
|-
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Converts an array of bytes to a string value.  You MUST end your char array with a char value of zero!
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Converts an array of bytes to a string value.  You MUST end your char array with a char value of zero!
Line 334: Line 499:
 
StringFromChars(str, test);  // copy chars from 'test' to 'str'
 
StringFromChars(str, test);  // copy chars from 'test' to 'str'
 
    
 
    
displayNextLCDString(str);   // display 'str' to line 3 of NXT LCD
+
while(true); // infinite loop (to keep debugger running)
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 346: Line 511:
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
| class="functionType"| <span class="bigKeywordBI">void </span><span class="bigKeywordB">strncat</span><span class="bigCodePunc">(</span><span class="bigKeywordBI">void </span><span class="bigCodeBasic">&pToBuffer</span><span class="bigCodePunc">, </span><span class="bigKeywordBI">const void </span><span class="bigCodeBasic">&pFromBuffer</span><span class="bigCodePunc">, </span><span class="bigKeywordBI">const short </span><span class="bigCodeBasic">nMaxBufferSize</span><span class="bigCodePunc">)</span>
+
| class="functionType"|  
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigKeywordB">strncat</span>
 +
<span class="bigCodePunc">(</span>
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigCodeBasic">&pToBuffer</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const void </span>
 +
<span class="bigCodeBasic">&pFromBuffer</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const short </span>
 +
<span class="bigCodeBasic">nMaxBufferSize</span>
 +
<span class="bigCodePunc">)</span>
 
|-
 
|-
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Function concatenates <span class="codeBasic">pFromBuffer</span> onto end of <span class="codeBasic">pToBuffer</span>. The variables are arrays of bytes terminated with a zero character. nMaxBufferSize is the maximum size of ‘pFromBuffer’ and is usually created with a <span class="keywordBI">sizeof</span><span class="codePunc">(</span><span class="codeBasic">..</span><span class="codePunc">)</span> function call. Identical to the function found in conventional C 'string.h' library file.
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Function concatenates <span class="codeBasic">pFromBuffer</span> onto end of <span class="codeBasic">pToBuffer</span>. The variables are arrays of bytes terminated with a zero character. nMaxBufferSize is the maximum size of ‘pFromBuffer’ and is usually created with a <span class="keywordBI">sizeof</span><span class="codePunc">(</span><span class="codeBasic">..</span><span class="codePunc">)</span> function call. Identical to the function found in conventional C 'string.h' library file.
Line 382: Line 559:
 
                                       from string 'str2' onto string 'str1'.        */
 
                                       from string 'str2' onto string 'str1'.        */
  
displayNextLCDString(str1);       // Display the new 'str' ("ROBOTC"), (the "!" is not copied).
+
while(true); // infinite loop (to keep debugger running)
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-
Line 393: Line 570:
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
| class="functionType"| <span class="bigKeywordBI">short </span><span class="bigKeywordB">strncmp</span><span class="bigCodePunc">(</span><span class="bigKeywordBI">void </span><span class="bigCodeBasic">&pString1</span><span class="bigCodePunc">, </span><span class="bigKeywordBI">const void </span><span class="bigCodeBasic">&pString2</span><span class="bigCodePunc">, </span><span class="bigKeywordBI">const short </span><span class="bigCodeBasic">nMaxBufferSize</span><span class="bigCodePunc">)</span>
+
| class="functionType"|  
 +
<span class="bigKeywordBI">short </span>
 +
<span class="bigKeywordB">strncmp</span>
 +
<span class="bigCodePunc">(</span>
 +
<span class="bigKeywordBI">void </span>
 +
<span class="bigCodeBasic">&pString1</span>
 +
<span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const void </span>
 +
<span class="bigCodeBasic">&pString2</span
 +
><span class="bigCodePunc">, </span>
 +
<span class="bigKeywordBI">const short </span>
 +
<span class="bigCodeBasic">nMaxBufferSize</span>
 +
<span class="bigCodePunc">)</span>
 
|-
 
|-
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_short|short]]) Function compares <span class="codeBasic">pString1</span> with <span class="codeBasic">pString2</span>. 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. <span class="codeBasic">nMaxBufferSize</span> is the maximum number of bytes to compare and is usually created with a <span class="keywordBI">sizeof</span><span class="codePunc">(</span><span class="codeBasic">..</span><span class="codePunc">)</span> function call. Identical to the function found in conventional C 'string.h'  
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_short|short]]) Function compares <span class="codeBasic">pString1</span> with <span class="codeBasic">pString2</span>. 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. <span class="codeBasic">nMaxBufferSize</span> is the maximum number of bytes to compare and is usually created with a <span class="keywordBI">sizeof</span><span class="codePunc">(</span><span class="codeBasic">..</span><span class="codePunc">)</span> function call. Identical to the function found in conventional C 'string.h'  
Line 428: Line 617:
 
                                               compare the first 4 chars of each string. */
 
                                               compare the first 4 chars of each string. */
  
if(strncmp(str1, str2, cmp_chars) < 0)                   // If 'str1' < 'str2' up to 4 chars:
+
if(strncmp(str1, str2, cmp_chars) < 0)       // If 'str1' < 'str2' up to 4 chars:
 
{
 
{
   nxtDisplayCenteredTextLine(3, "str1 is < than str2");    // Display that 'str1' is < '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:
+
else if(strncmp(str1, str2, cmp_chars) > 0)   // If 'str1' > 'str2' up to 4 chars:
 
{  
 
{  
   nxtDisplayCenteredTextLine(3, "str1 is > than str2");    // Display that 'str1' is > 'str2' up to 4 chars.
+
   // 'str1' is > 'str2' up to 4 chars.
 
}
 
}
else                                                     // If 'str1' == 'str2' up to 4 chars:
+
else                                         // If 'str1' == 'str2' up to 4 chars:
 
{
 
{
   nxtDisplayCenteredTextLine(3, "str1 = str2");            // Display that 'str1' is = 'str2' up to 4 chars.
+
   // 'str1' is = 'str2' up to 4 chars.
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 14:32, 21 May 2012


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. string
sFormatString, ... These are the source strings. string
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.
}